Initial working baulkTerm-daemon
[baulk.git] / src / Widgets / BaulkTerm / wrapper / baulkterm.cpp
blob0ddd35b1804fc76501b07ca779e158fd5571d383
1 // Baulk - qtermwidget Reimplement - BaulkTerm
2 //
3 // Baulk - Copyright (C) 2008 - Jacob Alexander
4 //
5 // Baulk is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // any later version, including version 3 of the License.
9 //
10 // Baulk is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "baulkterm.h"
20 // Constructors ***********************************************************************************
21 BaulkTerm::BaulkTerm( int startNow, QWidget *parent ) : BaulkWidget( parent ) {
22 // Tab Bar
23 //tabBar = new QTabBar( this );
24 //if ( !useTabBar )
25 //tabBar->hide();
27 tabLayer = new QStackedWidget;
29 // Initialize BaulkXML for Config Loading/Saving
30 xmlConfig = new BaulkXML( "BaulkTerm", this );
32 // term Settings
33 configurationDefaults();
34 configurationLoad();
36 // Initial Terminal
37 startPriority = startNow;
38 newTab();
40 // Layout
41 layout = new QVBoxLayout;
42 layout->setContentsMargins( 0,0,0,0 );
44 //layout->addWidget( tabBar );
46 layout->addWidget( tabLayer );
48 // Widget Settings
49 setFocusProxy( term );
50 setLayout( layout );
51 setWindowTitle( tr("BaulkTerm") );
53 // Connections
54 connect( term, SIGNAL( finished() ), this, SIGNAL( finished() ) );
55 connect( term, SIGNAL( finished() ), this, SLOT( closeTab() ) );
56 connect( term, SIGNAL( mouseSignal( int, int, int, int ) ), this, SLOT( xMouseInput( int, int, int, int ) ) );
57 connect( term, SIGNAL( rightClickAction() ), this, SLOT( rightClickAction() ) );
58 connect( term, SIGNAL( terminalTitleUpdate() ), this, SLOT( updateWindowTitle() ) );
61 // Configuration **********************************************************************************
62 void BaulkTerm::configurationDefaults() {
63 // Daemon
64 daemonEnabled = true;
66 // Font
67 font = QFont( "Terminus", 12 );
69 // Transparency
70 opacity = 0.7; // 70% opacity
71 fadeOpacity = 0.5; // 50% opacity
73 // History Size
74 historyType = "HistoryTypeNone";
75 historySize = 0; // No History
77 // Tabs
78 useTabBar = false; // TEMP
80 // Shell Program
81 shellProgram = "/bin/bash";
83 // Shell Environment - At least TERM should be set here
84 environmentVariables << "TERM=rxvt-unicode"
85 << "EDITOR=vim";
87 // Normal Colours
88 foreground .setOptions( QColor(0xD3,0xD3,0xD3), 0, 0 );
89 background .setOptions( QColor(0x00,0x00,0x00), 1, 0 );
90 black .setOptions( QColor(0x67,0x67,0x67), 0, 0 );
91 red .setOptions( QColor(0xEA,0x68,0x68), 0, 0 );
92 green .setOptions( QColor(0xAB,0xCB,0x8D), 0, 0 );
93 yellow .setOptions( QColor(0xE8,0xAE,0x5B), 0, 0 );
94 blue .setOptions( QColor(0x71,0xC5,0xF4), 0, 0 );
95 magenta .setOptions( QColor(0xE2,0xBA,0xF1), 0, 0 );
96 cyan .setOptions( QColor(0x21,0xF1,0xEA), 0, 0 );
97 white .setOptions( QColor(0xD3,0xD3,0xD3), 0, 0 );
99 // Intensive Colours
100 intForeground .setOptions( QColor(0x00,0x00,0x00), 0, 1 );
101 intBackground .setOptions( QColor(0xD3,0xD3,0xD3), 1, 0 );
102 intBlack .setOptions( QColor(0x75,0x75,0x75), 0, 0 );
103 intRed .setOptions( QColor(0xFF,0x72,0x72), 0, 0 );
104 intGreen .setOptions( QColor(0xAF,0xD7,0x8A), 0, 0 );
105 intYellow .setOptions( QColor(0xFF,0xA7,0x5D), 0, 0 );
106 intBlue .setOptions( QColor(0x67,0xCD,0xE9), 0, 0 );
107 intMagenta .setOptions( QColor(0xEC,0xAE,0xE9), 0, 0 );
108 intCyan .setOptions( QColor(0x36,0xFF,0xFC), 0, 0 );
109 intWhite .setOptions( QColor(0xFF,0xFF,0xFF), 0, 0 );
112 void BaulkTerm::configurationLoad() {
113 QVariant tmp;
115 opacity = ( tmp = xmlConfig->option("terminalOpacity") ) == QVariant("") ? opacity
116 : tmp.toDouble();
117 fadeOpacity = ( tmp = xmlConfig->option("terminalFadeOpacity") ) == QVariant("") ? fadeOpacity
118 : tmp.toDouble();
120 daemonEnabled = ( tmp = xmlConfig->option("terminalUseDaemon") ) == QVariant("") ? daemonEnabled
121 : tmp.toBool();
123 font.fromString( ( tmp = xmlConfig->option("terminalFont") ) == QVariant("") ? font.toString()
124 : tmp.toString() );
126 historyType = ( tmp = xmlConfig->option("terminalHistoryType") ) == QVariant("") ? historyType
127 : tmp.toString();
128 historySize = ( tmp = xmlConfig->option("terminalHistorySize") ) == QVariant("") ? historySize
129 : tmp.toInt();
131 shellProgram = ( tmp = xmlConfig->option("terminalShellProgram") ) == QVariant("") ? shellProgram
132 : tmp.toString();
133 environmentVariables = ( tmp = xmlConfig->option("terminalEnvironmentVariables") ) == QVariant("")
134 ? environmentVariables : tmp.toString().split(",");
136 // Normal Colours
137 foreground.setFromVariant(
138 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Foreground") ) ) == QVariant("")
139 ? foreground.toVariant() : tmp );
140 background.setFromVariant(
141 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Background") ) ) == QVariant("")
142 ? background.toVariant() : tmp );
143 black.setFromVariant(
144 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Black") ) ) == QVariant("")
145 ? black.toVariant() : tmp );
146 red.setFromVariant(
147 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Red") ) ) == QVariant("")
148 ? red.toVariant() : tmp );
149 green.setFromVariant(
150 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Green") ) ) == QVariant("")
151 ? green.toVariant() : tmp );
152 yellow.setFromVariant(
153 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Yellow") ) ) == QVariant("")
154 ? yellow.toVariant() : tmp );
155 blue.setFromVariant(
156 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Blue") ) ) == QVariant("")
157 ? blue.toVariant() : tmp );
158 magenta.setFromVariant(
159 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Magenta") ) ) == QVariant("")
160 ? magenta.toVariant() : tmp );
161 cyan.setFromVariant(
162 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Cyan") ) ) == QVariant("")
163 ? cyan.toVariant() : tmp );
164 white.setFromVariant(
165 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("White") ) ) == QVariant("")
166 ? white.toVariant() : tmp );
168 // Intensive Colours
169 intForeground.setFromVariant(
170 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Foreground") ) ) == QVariant("")
171 ? intForeground.toVariant() : tmp );
172 intBackground.setFromVariant(
173 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Background") ) ) == QVariant("")
174 ? intBackground.toVariant() : tmp );
175 intBlack.setFromVariant(
176 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Black") ) ) == QVariant("")
177 ? intBlack.toVariant() : tmp );
178 intRed.setFromVariant(
179 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Red") ) ) == QVariant("")
180 ? intRed.toVariant() : tmp );
181 intGreen.setFromVariant(
182 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Green") ) ) == QVariant("")
183 ? intGreen.toVariant() : tmp );
184 intYellow.setFromVariant(
185 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Yellow") ) ) == QVariant("")
186 ? intYellow.toVariant() : tmp );
187 intBlue.setFromVariant(
188 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Blue") ) ) == QVariant("")
189 ? intBlue.toVariant() : tmp );
190 intMagenta.setFromVariant(
191 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Magenta") ) ) == QVariant("")
192 ? intMagenta.toVariant() : tmp );
193 intCyan.setFromVariant(
194 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Cyan") ) ) == QVariant("")
195 ? intCyan.toVariant() : tmp );
196 intWhite.setFromVariant(
197 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("White") ) ) == QVariant("")
198 ? intWhite.toVariant() : tmp );
201 configurationSave();
204 void BaulkTerm::configurationSave() {
205 xmlConfig->setOption( "terminalOpacity", QVariant( opacity ) );
206 xmlConfig->setOption( "terminalFadeOpacity", QVariant( fadeOpacity ) );
207 xmlConfig->setOption( "terminalUseDaemon", QVariant( daemonEnabled ) );
208 xmlConfig->setOption( "terminalFont", QVariant( font ) );
209 xmlConfig->setOption( "terminalHistoryType", QVariant( historyType ) );
210 xmlConfig->setOption( "terminalHistorySize", QVariant( historySize ) );
211 xmlConfig->setOption( "terminalShellProgram", QVariant( shellProgram ) );
212 xmlConfig->setOption( "terminalEnvironmentVariables",
213 QVariant( environmentVariables.join(",") ) );
215 // Normal Colours
216 xmlConfig->setOption( "terminalColour", foreground.toVariant(),
217 "colour", QVariant("Foreground") );
218 xmlConfig->setOption( "terminalColour", background.toVariant(),
219 "colour", QVariant("Background") );
220 xmlConfig->setOption( "terminalColour", black.toVariant(),
221 "colour", QVariant("Black") );
222 xmlConfig->setOption( "terminalColour", red.toVariant(),
223 "colour", QVariant("Red") );
224 xmlConfig->setOption( "terminalColour", green.toVariant(),
225 "colour", QVariant("Green") );
226 xmlConfig->setOption( "terminalColour", yellow.toVariant(),
227 "colour", QVariant("Yellow") );
228 xmlConfig->setOption( "terminalColour", blue.toVariant(),
229 "colour", QVariant("Blue") );
230 xmlConfig->setOption( "terminalColour", magenta.toVariant(),
231 "colour", QVariant("Magenta") );
232 xmlConfig->setOption( "terminalColour", cyan.toVariant(),
233 "colour", QVariant("Cyan") );
234 xmlConfig->setOption( "terminalColour", white.toVariant(),
235 "colour", QVariant("White") );
237 // Intensive Colours
238 xmlConfig->setOption( "terminalColour", intForeground.toVariant(),
239 "intensiveColour", QVariant("Foreground") );
240 xmlConfig->setOption( "terminalColour", intBackground.toVariant(),
241 "intensiveColour", QVariant("Background") );
242 xmlConfig->setOption( "terminalColour", intBlack.toVariant(),
243 "intensiveColour", QVariant("Black") );
244 xmlConfig->setOption( "terminalColour", intRed.toVariant(),
245 "intensiveColour", QVariant("Red") );
246 xmlConfig->setOption( "terminalColour", intGreen.toVariant(),
247 "intensiveColour", QVariant("Green") );
248 xmlConfig->setOption( "terminalColour", intYellow.toVariant(),
249 "intensiveColour", QVariant("Yellow") );
250 xmlConfig->setOption( "terminalColour", intBlue.toVariant(),
251 "intensiveColour", QVariant("Blue") );
252 xmlConfig->setOption( "terminalColour", intMagenta.toVariant(),
253 "intensiveColour", QVariant("Magenta") );
254 xmlConfig->setOption( "terminalColour", intCyan.toVariant(),
255 "intensiveColour", QVariant("Cyan") );
256 xmlConfig->setOption( "terminalColour", intWhite.toVariant(),
257 "intensiveColour", QVariant("White") );
259 // Save Config to File
260 xmlConfig->saveConfig();
263 void BaulkTerm::configurationSet() {
264 term->setHistoryType( historyType, historySize );
265 term->setTerminalFont( font );
266 term->setOpacity( opacity );
267 term->setShellProgram( shellProgram );
268 term->setEnvironment( environmentVariables );
270 // Normal Colours
271 term->setColor( 0, foreground.colour(), foreground.transparency(), foreground.bold() );
272 term->setColor( 1, background.colour(), background.transparency(), background.bold() );
273 term->setColor( 2, black.colour(), black.transparency(), black.bold() );
274 term->setColor( 3, red.colour(), red.transparency(), red.bold() );
275 term->setColor( 4, green.colour(), green.transparency(), green.bold() );
276 term->setColor( 5, yellow.colour(), yellow.transparency(), yellow.bold() );
277 term->setColor( 6, blue.colour(), blue.transparency(), blue.bold() );
278 term->setColor( 7, magenta.colour(), magenta.transparency(), magenta.bold() );
279 term->setColor( 8, cyan.colour(), cyan.transparency(), cyan.bold() );
280 term->setColor( 9, white.colour(), white.transparency(), white.bold() );
282 // Intensive Colours
283 term->setColor( 10, foreground.colour(), foreground.transparency(), foreground.bold() );
284 term->setColor( 11, background.colour(), background.transparency(), background.bold() );
285 term->setColor( 12, black.colour(), black.transparency(), black.bold() );
286 term->setColor( 13, red.colour(), red.transparency(), red.bold() );
287 term->setColor( 14, green.colour(), green.transparency(), green.bold() );
288 term->setColor( 15, yellow.colour(), yellow.transparency(), yellow.bold() );
289 term->setColor( 16, blue.colour(), blue.transparency(), blue.bold() );
290 term->setColor( 17, magenta.colour(), magenta.transparency(), magenta.bold() );
291 term->setColor( 18, cyan.colour(), cyan.transparency(), cyan.bold() );
292 term->setColor( 19, white.colour(), white.transparency(), white.bold() );
295 // X Input ****************************************************************************************
296 void BaulkTerm::xMouseInput( int button, int column, int line, int eventType ) {
297 // This will only signal if the terminal application supports an X Mouse
298 qDebug( QString("Mouse - Button %1 | Column %2 | Line %3 | eventType %4")
299 .arg( button )
300 .arg( column )
301 .arg( line )
302 .arg( eventType ).toUtf8() );
305 // Configuration Menus ****************************************************************************
306 void BaulkTerm::rightClickAction() {
307 qDebug("RIGHT");
310 // Tabs *******************************************************************************************
311 void BaulkTerm::newTab() {
312 term = new QTermWidget( startPriority, this );
314 // Use Current Configuration Options on new terminal
315 configurationSet();
317 // Add terminal to tabbed view
318 tabLayer->addWidget( term );
319 //tabBar->addTab( QString("Terminal %1").arg( tabLayer->count() ) );
321 term->updateImage();
324 void BaulkTerm::closeTab() {
325 // Remove terminal from tabs
326 //tabBar->removeTab( tabLayer->currentIndex() );
327 tabLayer->removeWidget( term );
329 // Close Terminal if there are no more tabs
330 if ( tabLayer->count() < 1 )
331 close();
334 // New Terminal ***********************************************************************************
335 void BaulkTerm::newTerminal() {
336 qDebug("NEWTERM!!");
337 term = (QTermWidget*)createTermWidget( 1, 0 );
339 configurationSet();
340 term->startShellProgram();
341 term->show();
344 // QTermWidget Passthrough Options ****************************************************************
345 void BaulkTerm::startShellProgram() {
346 term->startShellProgram();
349 // Terminal Title *********************************************************************************
350 void BaulkTerm::updateWindowTitle() {
351 setWindowTitle( term->terminalTitle() );
354 // Reimplemented Functions ************************************************************************
355 void BaulkTerm::changeEvent( QEvent *event ) {
356 switch ( event->type() ) {
357 case QEvent::ActivationChange:
358 // Terminal has focus - unfade
359 if ( isActiveWindow() )
360 term->setOpacity( opacity );
361 // Terminal lost focus - fade
362 else
363 term->setOpacity( fadeOpacity );
364 break;
365 default:
366 qDebug("Default - %d", event->type() );
367 break;
369 event->accept();
372 bool BaulkTerm::processCommandArgs() {
373 // Note: Options that are decyphered are removed from the arguments list
374 bool runApp = true;
375 QStringList args = qApp->arguments();
377 // Help
378 if ( args.contains( tr("--h" ) ) || args.contains( tr("--help") ) ) {
379 QString out = tr(
380 "BaulkTerm - A Konsole based Qt4 terminal emulator\n"
381 "Note: Some options may not be implemented yet\n"
382 "Usage:\n"
383 " baulkTerm [OPTION...]\n"
384 "\n"
385 "Usual Options:\n"
386 " --h, --help Show help options (this list)\n"
387 " --v, --version BaulkTerm version\n"
388 "\n"
389 "Application Options:\n"
390 " Note: These options override the configuration file\n"
391 " --columns Set number of columns\n"
392 " --e, --execute Execute command\n"
393 " --font <font family> <font size>\n"
394 " Terminal font used\n"
395 " --shell <shell program> ie. /bin/bash, /bin/zsh, etc.\n"
396 " --rows Set number of rows\n"
398 std::cout << out.toUtf8().data();
399 return false;
402 // Version
403 if ( args.contains( tr("--v") ) || args.contains( tr("--version") ) ) {
404 QString out = tr(
405 "BaulkTerm Version 0.1.git(%1)\n"
406 ).arg( "TODO" );
407 std::cout << out.toUtf8().data();
408 return false;
411 // Columns and Rows
412 QString columns = tr("--columns");
413 QString rows = tr("--rows");
414 if ( args.contains( columns ) || args.contains( rows ) ) {
415 bool ok;
416 int horizontal = 80;
417 int vertical = 60;
418 for ( int c = 0; c + 1 < args.count(); ++c ) {
419 if ( args[c] == columns ) {
420 horizontal = args[c + 1].toInt( &ok );
421 if ( ok ) {
422 args.removeAt( c );
423 args.removeAt( c );
424 --c;
425 if ( c + 1 >= args.count() )
426 break;
429 if ( args[c] == rows ) {
430 vertical = args[c + 1].toInt( &ok );
431 if ( ok ) {
432 args.removeAt( c );
433 args.removeAt( c );
434 --c;
435 if ( c + 1 >= args.count() )
436 break;
441 qWarning("This feature isn't used by the developer that much, log bugs if it doesn't work");
442 term->setSize( horizontal, vertical );
445 // Font
446 QString font = tr("--font");
447 if ( args.contains( font ) ) {
448 for ( int c = 0; c + 2 < args.count(); ++c ) {
449 if ( args[c] == font ) {
450 bool ok;
452 QFont termFont( args[c + 1], QString( args[c + 2] ).toInt( &ok ) );
453 if ( !ok ) {
454 qFatal( tr("Invalid font size - %1").arg( args[c + 2] ).toUtf8() );
455 return false;
458 term->setTerminalFont( termFont );
460 args.removeAt( c );
461 args.removeAt( c );
462 args.removeAt( c );
467 // Shell Program
468 QString shell = tr("--shell");
469 if ( args.contains( shell ) ) {
470 for ( int c = 0; c + 1 < args.count(); ++c ) {
471 if ( args[c] == shell ) {
472 term->setShellProgram( args[c + 1] );
473 args.removeAt( c );
474 args.removeAt( c );
479 // Execute Command (assumed last command decyphered)
480 if ( args.contains( tr("--e") ) || args.contains( tr("--execute") ) ) {
481 QString command;
483 int count = 2;
484 while ( count < args.count() ) {
485 command += args[count] + " ";
486 args.removeAt( count );
489 args.removeAt( 1 );
491 qDebug( tr("Executing - %1").arg( command ).toUtf8() );
492 command += "\n";
493 term->sendText( command );
496 if ( args.count() > 1 ) {
497 qFatal( tr("Invalid Command-Line Argument(s) - %1").arg( args.count() - 1 ).toUtf8() );
498 return false;
501 return runApp;
504 void BaulkTerm::resizeEvent( QResizeEvent *event ) {
505 term->updateImage();
506 event->accept();