Daemon mode is now usable
[baulk.git] / src / Widgets / BaulkTerm / wrapper / baulkterm.cpp
blobfe21ef0a3e6c37230c2bf2012e946d67bbd86a9e
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 // Initialize BaulkXML for Config Loading/Saving
23 xmlConfig = new BaulkXML( "BaulkTerm", this );
25 // term Settings
26 configurationDefaults();
27 configurationLoad();
29 // Initial Terminal
30 startPriority = startNow;
32 setStyleSheet("QWidget {"
33 "background: black;"
34 "}");
37 // Configuration **********************************************************************************
38 void BaulkTerm::configurationDefaults() {
39 // Daemon
40 daemonEnabled = true;
41 daemonListenName = "BaulkTermServer";
43 // Font
44 font = QFont( "Terminus", 12 );
46 // Transparency
47 opacity = 0.7; // 70% opacity
48 fadeOpacity = 0.5; // 50% opacity
50 // History Size
51 historyType = "HistoryTypeNone";
52 historySize = 0; // No History
54 // Tabs
55 useTabBar = false; // TEMP
57 // Shell Program
58 shellProgram = "/bin/bash";
60 // Shell Environment - At least TERM should be set here
61 environmentVariables << "TERM=rxvt-unicode"
62 << "EDITOR=vim";
64 // Normal Colours
65 foreground .setOptions( QColor(0xD3,0xD3,0xD3), 0, 0 );
66 background .setOptions( QColor(0x00,0x00,0x00), 1, 0 );
67 black .setOptions( QColor(0x67,0x67,0x67), 0, 0 );
68 red .setOptions( QColor(0xEA,0x68,0x68), 0, 0 );
69 green .setOptions( QColor(0xAB,0xCB,0x8D), 0, 0 );
70 yellow .setOptions( QColor(0xE8,0xAE,0x5B), 0, 0 );
71 blue .setOptions( QColor(0x71,0xC5,0xF4), 0, 0 );
72 magenta .setOptions( QColor(0xE2,0xBA,0xF1), 0, 0 );
73 cyan .setOptions( QColor(0x21,0xF1,0xEA), 0, 0 );
74 white .setOptions( QColor(0xD3,0xD3,0xD3), 0, 0 );
76 // Intensive Colours
77 intForeground .setOptions( QColor(0x00,0x00,0x00), 0, 1 );
78 intBackground .setOptions( QColor(0xD3,0xD3,0xD3), 1, 0 );
79 intBlack .setOptions( QColor(0x75,0x75,0x75), 0, 0 );
80 intRed .setOptions( QColor(0xFF,0x72,0x72), 0, 0 );
81 intGreen .setOptions( QColor(0xAF,0xD7,0x8A), 0, 0 );
82 intYellow .setOptions( QColor(0xFF,0xA7,0x5D), 0, 0 );
83 intBlue .setOptions( QColor(0x67,0xCD,0xE9), 0, 0 );
84 intMagenta .setOptions( QColor(0xEC,0xAE,0xE9), 0, 0 );
85 intCyan .setOptions( QColor(0x36,0xFF,0xFC), 0, 0 );
86 intWhite .setOptions( QColor(0xFF,0xFF,0xFF), 0, 0 );
89 void BaulkTerm::configurationLoad() {
90 QVariant tmp;
92 opacity = ( tmp = xmlConfig->option("terminalOpacity") ) == QVariant("") ? opacity
93 : tmp.toDouble();
94 fadeOpacity = ( tmp = xmlConfig->option("terminalFadeOpacity") ) == QVariant("") ? fadeOpacity
95 : tmp.toDouble();
97 daemonEnabled = ( tmp = xmlConfig->option("terminalUseDaemon") ) == QVariant("") ? daemonEnabled
98 : tmp.toBool();
99 daemonListenName = ( tmp = xmlConfig->option("terminalDaemonListenName") ) == QVariant("")
100 ? daemonListenName : tmp.toString();
102 font.fromString( ( tmp = xmlConfig->option("terminalFont") ) == QVariant("") ? font.toString()
103 : tmp.toString() );
105 historyType = ( tmp = xmlConfig->option("terminalHistoryType") ) == QVariant("") ? historyType
106 : tmp.toString();
107 historySize = ( tmp = xmlConfig->option("terminalHistorySize") ) == QVariant("") ? historySize
108 : tmp.toInt();
110 shellProgram = ( tmp = xmlConfig->option("terminalShellProgram") ) == QVariant("") ? shellProgram
111 : tmp.toString();
112 environmentVariables = ( tmp = xmlConfig->option("terminalEnvironmentVariables") ) == QVariant("")
113 ? environmentVariables : tmp.toString().split(",");
115 // Normal Colours
116 foreground.setFromVariant(
117 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Foreground") ) ) == QVariant("")
118 ? foreground.toVariant() : tmp );
119 background.setFromVariant(
120 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Background") ) ) == QVariant("")
121 ? background.toVariant() : tmp );
122 black.setFromVariant(
123 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Black") ) ) == QVariant("")
124 ? black.toVariant() : tmp );
125 red.setFromVariant(
126 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Red") ) ) == QVariant("")
127 ? red.toVariant() : tmp );
128 green.setFromVariant(
129 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Green") ) ) == QVariant("")
130 ? green.toVariant() : tmp );
131 yellow.setFromVariant(
132 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Yellow") ) ) == QVariant("")
133 ? yellow.toVariant() : tmp );
134 blue.setFromVariant(
135 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Blue") ) ) == QVariant("")
136 ? blue.toVariant() : tmp );
137 magenta.setFromVariant(
138 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Magenta") ) ) == QVariant("")
139 ? magenta.toVariant() : tmp );
140 cyan.setFromVariant(
141 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("Cyan") ) ) == QVariant("")
142 ? cyan.toVariant() : tmp );
143 white.setFromVariant(
144 ( tmp = xmlConfig->option( "terminalColour", "colour", QVariant("White") ) ) == QVariant("")
145 ? white.toVariant() : tmp );
147 // Intensive Colours
148 intForeground.setFromVariant(
149 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Foreground") ) ) == QVariant("")
150 ? intForeground.toVariant() : tmp );
151 intBackground.setFromVariant(
152 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Background") ) ) == QVariant("")
153 ? intBackground.toVariant() : tmp );
154 intBlack.setFromVariant(
155 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Black") ) ) == QVariant("")
156 ? intBlack.toVariant() : tmp );
157 intRed.setFromVariant(
158 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Red") ) ) == QVariant("")
159 ? intRed.toVariant() : tmp );
160 intGreen.setFromVariant(
161 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Green") ) ) == QVariant("")
162 ? intGreen.toVariant() : tmp );
163 intYellow.setFromVariant(
164 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Yellow") ) ) == QVariant("")
165 ? intYellow.toVariant() : tmp );
166 intBlue.setFromVariant(
167 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Blue") ) ) == QVariant("")
168 ? intBlue.toVariant() : tmp );
169 intMagenta.setFromVariant(
170 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Magenta") ) ) == QVariant("")
171 ? intMagenta.toVariant() : tmp );
172 intCyan.setFromVariant(
173 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("Cyan") ) ) == QVariant("")
174 ? intCyan.toVariant() : tmp );
175 intWhite.setFromVariant(
176 ( tmp = xmlConfig->option( "terminalColour", "intensiveColour", QVariant("White") ) ) == QVariant("")
177 ? intWhite.toVariant() : tmp );
180 configurationSave();
183 void BaulkTerm::configurationSave() {
184 xmlConfig->setOption( "terminalOpacity", QVariant( opacity ) );
185 xmlConfig->setOption( "terminalFadeOpacity", QVariant( fadeOpacity ) );
186 xmlConfig->setOption( "terminalUseDaemon", QVariant( daemonEnabled ) );
187 xmlConfig->setOption( "terminalDaemonListenName", QVariant( daemonListenName ) );
188 xmlConfig->setOption( "terminalFont", QVariant( font ) );
189 xmlConfig->setOption( "terminalHistoryType", QVariant( historyType ) );
190 xmlConfig->setOption( "terminalHistorySize", QVariant( historySize ) );
191 xmlConfig->setOption( "terminalShellProgram", QVariant( shellProgram ) );
192 xmlConfig->setOption( "terminalEnvironmentVariables",
193 QVariant( environmentVariables.join(",") ) );
195 // Normal Colours
196 xmlConfig->setOption( "terminalColour", foreground.toVariant(),
197 "colour", QVariant("Foreground") );
198 xmlConfig->setOption( "terminalColour", background.toVariant(),
199 "colour", QVariant("Background") );
200 xmlConfig->setOption( "terminalColour", black.toVariant(),
201 "colour", QVariant("Black") );
202 xmlConfig->setOption( "terminalColour", red.toVariant(),
203 "colour", QVariant("Red") );
204 xmlConfig->setOption( "terminalColour", green.toVariant(),
205 "colour", QVariant("Green") );
206 xmlConfig->setOption( "terminalColour", yellow.toVariant(),
207 "colour", QVariant("Yellow") );
208 xmlConfig->setOption( "terminalColour", blue.toVariant(),
209 "colour", QVariant("Blue") );
210 xmlConfig->setOption( "terminalColour", magenta.toVariant(),
211 "colour", QVariant("Magenta") );
212 xmlConfig->setOption( "terminalColour", cyan.toVariant(),
213 "colour", QVariant("Cyan") );
214 xmlConfig->setOption( "terminalColour", white.toVariant(),
215 "colour", QVariant("White") );
217 // Intensive Colours
218 xmlConfig->setOption( "terminalColour", intForeground.toVariant(),
219 "intensiveColour", QVariant("Foreground") );
220 xmlConfig->setOption( "terminalColour", intBackground.toVariant(),
221 "intensiveColour", QVariant("Background") );
222 xmlConfig->setOption( "terminalColour", intBlack.toVariant(),
223 "intensiveColour", QVariant("Black") );
224 xmlConfig->setOption( "terminalColour", intRed.toVariant(),
225 "intensiveColour", QVariant("Red") );
226 xmlConfig->setOption( "terminalColour", intGreen.toVariant(),
227 "intensiveColour", QVariant("Green") );
228 xmlConfig->setOption( "terminalColour", intYellow.toVariant(),
229 "intensiveColour", QVariant("Yellow") );
230 xmlConfig->setOption( "terminalColour", intBlue.toVariant(),
231 "intensiveColour", QVariant("Blue") );
232 xmlConfig->setOption( "terminalColour", intMagenta.toVariant(),
233 "intensiveColour", QVariant("Magenta") );
234 xmlConfig->setOption( "terminalColour", intCyan.toVariant(),
235 "intensiveColour", QVariant("Cyan") );
236 xmlConfig->setOption( "terminalColour", intWhite.toVariant(),
237 "intensiveColour", QVariant("White") );
239 // Save Config to File
240 xmlConfig->saveConfig();
243 void BaulkTerm::configurationSet() {
244 term->setHistoryType( historyType, historySize );
245 term->setTerminalFont( font );
246 term->setOpacity( opacity );
247 term->setShellProgram( shellProgram );
248 term->setEnvironment( environmentVariables );
250 // Normal Colours
251 term->setColor( 0, foreground.colour(), foreground.transparency(), foreground.bold() );
252 term->setColor( 1, background.colour(), background.transparency(), background.bold() );
253 term->setColor( 2, black.colour(), black.transparency(), black.bold() );
254 term->setColor( 3, red.colour(), red.transparency(), red.bold() );
255 term->setColor( 4, green.colour(), green.transparency(), green.bold() );
256 term->setColor( 5, yellow.colour(), yellow.transparency(), yellow.bold() );
257 term->setColor( 6, blue.colour(), blue.transparency(), blue.bold() );
258 term->setColor( 7, magenta.colour(), magenta.transparency(), magenta.bold() );
259 term->setColor( 8, cyan.colour(), cyan.transparency(), cyan.bold() );
260 term->setColor( 9, white.colour(), white.transparency(), white.bold() );
262 // Intensive Colours
263 term->setColor( 10, foreground.colour(), foreground.transparency(), foreground.bold() );
264 term->setColor( 11, background.colour(), background.transparency(), background.bold() );
265 term->setColor( 12, black.colour(), black.transparency(), black.bold() );
266 term->setColor( 13, red.colour(), red.transparency(), red.bold() );
267 term->setColor( 14, green.colour(), green.transparency(), green.bold() );
268 term->setColor( 15, yellow.colour(), yellow.transparency(), yellow.bold() );
269 term->setColor( 16, blue.colour(), blue.transparency(), blue.bold() );
270 term->setColor( 17, magenta.colour(), magenta.transparency(), magenta.bold() );
271 term->setColor( 18, cyan.colour(), cyan.transparency(), cyan.bold() );
272 term->setColor( 19, white.colour(), white.transparency(), white.bold() );
275 // X Input ****************************************************************************************
276 void BaulkTerm::xMouseInput( int button, int column, int line, int eventType ) {
277 // This will only signal if the terminal application supports an X Mouse
278 qDebug( QString("Mouse - Button %1 | Column %2 | Line %3 | eventType %4")
279 .arg( button )
280 .arg( column )
281 .arg( line )
282 .arg( eventType ).toUtf8() );
285 // Configuration Menus ****************************************************************************
286 void BaulkTerm::rightClickAction() {
287 qDebug("RIGHT");
290 // Tabs *******************************************************************************************
291 void BaulkTerm::newTab() {
292 term = new QTermWidget( startPriority, this );
294 // Use Current Configuration Options on new terminal
295 configurationSet();
297 term->updateImage();
300 void BaulkTerm::closeTab() {
303 // Terminal Start/Close ***************************************************************************
304 void BaulkTerm::newTerminal() {
305 // New Terminal
306 term = new QTermWidget( startPriority, this );
307 termList.append( term );
309 // Dialog for Terminal Layout
310 if ( layout() == 0 ) {
311 // Layout for Terminal Widget
312 termLayout = new QVBoxLayout;
313 termLayout->setContentsMargins( 0,0,0,0 );
314 termLayout->addWidget( term );
316 // Hide the main window if the terminal contained is finished
317 connect( term, SIGNAL( finished() ), this, SLOT( hide() ) );
318 connect( term, SIGNAL( finished() ), termLayout, SLOT( deleteLater() ) );
320 // Widget Settings
321 setWindowTitle( tr("BaulkTerm") );
323 // Layout Setup
324 setLayout( termLayout );
325 show();
327 qDebug("MAIN");
329 else {
330 // Use a new Window
331 QMainWindow *window = new QMainWindow( this );
333 // Hide and Close the Dialog window once the terminal is finished
334 connect( term, SIGNAL( finished() ), window, SLOT( hide() ) );
335 connect( term, SIGNAL( finished() ), window, SLOT( close() ) );
337 // Widget Settings
338 window->setWindowTitle( tr("BaulkTerm") );
340 // Layout Setup
341 window->setCentralWidget( term );
342 window->show();
344 qDebug("EXTRA");
347 // Apply Terminal Settings
348 configurationSet();
350 // Connections
351 connect( term, SIGNAL( finished() ), this, SIGNAL( finished() ) );
352 connect( term, SIGNAL( finished() ), this, SLOT( removeTerminalFromList() ) );
353 connect( term, SIGNAL( mouseSignal( int, int, int, int ) ), this, SLOT( xMouseInput( int, int, int, int ) ) );
354 connect( term, SIGNAL( rightClickAction() ), this, SLOT( rightClickAction() ) );
355 connect( term, SIGNAL( terminalTitleUpdate() ), this, SLOT( updateWindowTitle() ) );
357 startShellProgram();
359 term->updateImage();
362 void BaulkTerm::removeTerminalFromList() {
363 qDebug( "%d", termList.indexOf( 0 ) );
366 // QTermWidget Passthrough Options ****************************************************************
367 void BaulkTerm::startShellProgram() {
368 term->startShellProgram();
371 // Terminal Title *********************************************************************************
372 void BaulkTerm::updateWindowTitle() {
373 setWindowTitle( term->terminalTitle() );
376 // Reimplemented Functions ************************************************************************
377 void BaulkTerm::changeEvent( QEvent *event ) {
378 switch ( event->type() ) {
379 case QEvent::ActivationChange:
380 // Terminal has focus - unfade
381 if ( isActiveWindow() )
382 term->setOpacity( opacity );
383 // Terminal lost focus - fade
384 else
385 term->setOpacity( fadeOpacity );
386 break;
387 default:
388 qDebug("Default - %d", event->type() );
389 break;
391 event->accept();
394 bool BaulkTerm::processCommandArgs() {
395 // Note: Options that are decyphered are removed from the arguments list
396 bool runApp = true;
397 QStringList args = qApp->arguments();
399 // Help
400 if ( args.contains( tr("--h" ) ) || args.contains( tr("--help") ) ) {
401 QString out = tr(
402 "BaulkTerm - A Konsole based Qt4 terminal emulator\n"
403 "Note: Some options may not be implemented yet\n"
404 "Usage:\n"
405 " baulkTerm [OPTION...]\n"
406 "\n"
407 "Usual Options:\n"
408 " --h, --help Show help options (this list)\n"
409 " --v, --version BaulkTerm version\n"
410 "\n"
411 "Application Options:\n"
412 " Note: These options override the configuration file\n"
413 " --columns Set number of columns\n"
414 " --e, --execute Execute command\n"
415 " --font <font family> <font size>\n"
416 " Terminal font used\n"
417 " --shell <shell program> ie. /bin/bash, /bin/zsh, etc.\n"
418 " --rows Set number of rows\n"
420 std::cout << out.toUtf8().data();
421 return false;
424 // Version
425 if ( args.contains( tr("--v") ) || args.contains( tr("--version") ) ) {
426 QString out = tr(
427 "BaulkTerm Version 0.1.git(%1)\n"
428 ).arg( "TODO" );
429 std::cout << out.toUtf8().data();
430 return false;
433 // Columns and Rows
434 QString columns = tr("--columns");
435 QString rows = tr("--rows");
436 if ( args.contains( columns ) || args.contains( rows ) ) {
437 bool ok;
438 int horizontal = 80;
439 int vertical = 60;
440 for ( int c = 0; c + 1 < args.count(); ++c ) {
441 if ( args[c] == columns ) {
442 horizontal = args[c + 1].toInt( &ok );
443 if ( ok ) {
444 args.removeAt( c );
445 args.removeAt( c );
446 --c;
447 if ( c + 1 >= args.count() )
448 break;
451 if ( args[c] == rows ) {
452 vertical = args[c + 1].toInt( &ok );
453 if ( ok ) {
454 args.removeAt( c );
455 args.removeAt( c );
456 --c;
457 if ( c + 1 >= args.count() )
458 break;
463 qWarning("This feature isn't used by the developer that much, log bugs if it doesn't work");
464 term->setSize( horizontal, vertical );
467 // Font
468 QString font = tr("--font");
469 if ( args.contains( font ) ) {
470 for ( int c = 0; c + 2 < args.count(); ++c ) {
471 if ( args[c] == font ) {
472 bool ok;
474 QFont termFont( args[c + 1], QString( args[c + 2] ).toInt( &ok ) );
475 if ( !ok ) {
476 qFatal( tr("Invalid font size - %1").arg( args[c + 2] ).toUtf8() );
477 return false;
480 term->setTerminalFont( termFont );
482 args.removeAt( c );
483 args.removeAt( c );
484 args.removeAt( c );
489 // Shell Program
490 QString shell = tr("--shell");
491 if ( args.contains( shell ) ) {
492 for ( int c = 0; c + 1 < args.count(); ++c ) {
493 if ( args[c] == shell ) {
494 term->setShellProgram( args[c + 1] );
495 args.removeAt( c );
496 args.removeAt( c );
501 // Execute Command (assumed last command decyphered)
502 if ( args.contains( tr("--e") ) || args.contains( tr("--execute") ) ) {
503 QString command;
505 int count = 2;
506 while ( count < args.count() ) {
507 command += args[count] + " ";
508 args.removeAt( count );
511 args.removeAt( 1 );
513 qDebug( tr("Executing - %1").arg( command ).toUtf8() );
514 command += "\n";
515 term->sendText( command );
518 if ( args.count() > 1 ) {
519 qFatal( tr("Invalid Command-Line Argument(s) - %1").arg( args.count() - 1 ).toUtf8() );
520 return false;
523 return runApp;
526 void BaulkTerm::resizeEvent( QResizeEvent *event ) {
527 term->updateImage();
528 event->accept();