~larryprice/acolyterm/release-0.1

« back to all changes in this revision

Viewing changes to src/plugin/konsole/Session.h

  • Committer: Larry Price
  • Date: 2016-06-15 14:47:59 UTC
  • Revision ID: larry.price@canonical.com-20160615144759-6wopn0gxwgta3x1n
Updating QMLTermWidget and removing unnecessary konsole codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of Konsole, an X terminal.
3
 
 
4
 
    Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
5
 
    Copyright (C) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
6
 
 
7
 
    Rewritten for QT4     by e_k      <e_k at users.sourceforge.net>, Copyright (C) 2008
8
 
    Rewritten for QT5/QML by Dmitry Zagnoyko   <hiroshidi@gmail.com>, Copyright (C) 2013
9
 
 
10
 
    This program is free software; you can redistribute it and/or modify
11
 
    it under the terms of the GNU General Public License as published by
12
 
    the Free Software Foundation; either version 2 of the License, or
13
 
    (at your option) any later version.
14
 
 
15
 
    This program is distributed in the hope that it will be useful,
16
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
    GNU General Public License for more details.
19
 
 
20
 
    You should have received a copy of the GNU General Public License
21
 
    along with this program; if not, write to the Free Software
22
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23
 
    02110-1301  USA.
24
 
*/
25
 
 
26
 
#ifndef SESSION_H
27
 
#define SESSION_H
28
 
 
29
 
 
30
 
#include <QStringList>
31
 
 
32
 
#include <QtCore/QTimer>
33
 
#include <QtGui/QWindow>
34
 
 
35
 
#include "History.h"
36
 
 
37
 
class KProcess;
38
 
 
39
 
 
40
 
class Emulation;
41
 
class Pty;
42
 
class KTerminalDisplay;
43
 
//class ZModemDialog;
44
 
 
45
 
/**
46
 
 * Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
47
 
 * The pseudo-teletype (or PTY) handles I/O between the terminal process and Konsole.
48
 
 * The terminal emulation ( Emulation and subclasses ) processes the output stream from the
49
 
 * PTY and produces a character image which is then shown on views connected to the session.
50
 
 *
51
 
 * Each Session can be connected to one or more views by using the addView() method.
52
 
 * The attached views can then display output from the program running in the terminal
53
 
 * or send input to the program in the terminal in the form of keypresses and mouse
54
 
 * activity.
55
 
 */
56
 
class Session : public QObject {
57
 
    Q_OBJECT
58
 
 
59
 
public:
60
 
    Q_PROPERTY(QString name READ nameTitle)
61
 
    Q_PROPERTY(int processId READ processId)
62
 
    Q_PROPERTY(QString keyBindings READ keyBindings WRITE setKeyBindings)
63
 
    Q_PROPERTY(QSize size READ size WRITE setSize)
64
 
 
65
 
    /**
66
 
     * Constructs a new session.
67
 
     *
68
 
     * To start the terminal process, call the run() method,
69
 
     * after specifying the program and arguments
70
 
     * using setProgram() and setArguments()
71
 
     *
72
 
     * If no program or arguments are specified explicitly, the Session
73
 
     * falls back to using the program specified in the SHELL environment
74
 
     * variable.
75
 
     */
76
 
    Session();
77
 
    ~Session();
78
 
 
79
 
    /**
80
 
     * Returns true if the session is currently running.  This will be true
81
 
     * after run() has been called successfully.
82
 
     */
83
 
    bool isRunning() const;
84
 
 
85
 
    /**
86
 
     * Sets the profile associated with this session.
87
 
     *
88
 
     * @param profileKey A key which can be used to obtain the current
89
 
     * profile settings from the SessionManager
90
 
     */
91
 
    void setProfileKey(const QString & profileKey);
92
 
    /**
93
 
     * Returns the profile key associated with this session.
94
 
     * This can be passed to the SessionManager to obtain the current
95
 
     * profile settings.
96
 
     */
97
 
    QString profileKey() const;
98
 
 
99
 
    /**
100
 
     * Adds a new view for this session.
101
 
     *
102
 
     * The viewing widget will display the output from the terminal and
103
 
     * input from the viewing widget (key presses, mouse activity etc.)
104
 
     * will be sent to the terminal.
105
 
     *
106
 
     * Views can be removed using removeView().  The session is automatically
107
 
     * closed when the last view is removed.
108
 
     */
109
 
    void addView(KTerminalDisplay * widget);
110
 
    /**
111
 
     * Removes a view from this session.  When the last view is removed,
112
 
     * the session will be closed automatically.
113
 
     *
114
 
     * @p widget will no longer display output from or send input
115
 
     * to the terminal
116
 
     */
117
 
    void removeView(KTerminalDisplay * widget);
118
 
 
119
 
    /**
120
 
     * Returns the views connected to this session
121
 
     */
122
 
    QList<KTerminalDisplay *> views() const;
123
 
 
124
 
    /**
125
 
     * Returns the terminal emulation instance being used to encode / decode
126
 
     * characters to / from the process.
127
 
     */
128
 
    Emulation * emulation() const;
129
 
 
130
 
    /**
131
 
     * Returns the environment of this session as a list of strings like
132
 
     * VARIABLE=VALUE
133
 
     */
134
 
    QStringList environment() const;
135
 
    /**
136
 
     * Sets the environment for this session.
137
 
     * @p environment should be a list of strings like
138
 
     * VARIABLE=VALUE
139
 
     */
140
 
    void setEnvironment(const QStringList & environment);
141
 
 
142
 
    /** Returns the unique ID for this session. */
143
 
    int sessionId() const;
144
 
 
145
 
    /**
146
 
     * Return the session title set by the user (ie. the program running
147
 
     * in the terminal), or an empty string if the user has not set a custom title
148
 
     */
149
 
    QString userTitle() const;
150
 
 
151
 
    /**
152
 
     * This enum describes the contexts for which separate
153
 
     * tab title formats may be specified.
154
 
     */
155
 
    enum TabTitleContext {
156
 
        /** Default tab title format */
157
 
        LocalTabTitle,
158
 
        /**
159
 
         * Tab title format used session currently contains
160
 
         * a connection to a remote computer (via SSH)
161
 
         */
162
 
        RemoteTabTitle
163
 
    };
164
 
    /**
165
 
     * Sets the format used by this session for tab titles.
166
 
     *
167
 
     * @param context The context whoose format should be set.
168
 
     * @param format The tab title format.  This may be a mixture
169
 
     * of plain text and dynamic elements denoted by a '%' character
170
 
     * followed by a letter.  (eg. %d for directory).  The dynamic
171
 
     * elements available depend on the @p context
172
 
     */
173
 
    void setTabTitleFormat(TabTitleContext context , const QString & format);
174
 
    /** Returns the format used by this session for tab titles. */
175
 
    QString tabTitleFormat(TabTitleContext context) const;
176
 
 
177
 
 
178
 
    /** Returns the arguments passed to the shell process when run() is called. */
179
 
    QStringList arguments() const;
180
 
    /** Returns the program name of the shell process started when run() is called. */
181
 
    QString program() const;
182
 
 
183
 
    /**
184
 
     * Sets the command line arguments which the session's program will be passed when
185
 
     * run() is called.
186
 
     */
187
 
    void setArguments(const QStringList & arguments);
188
 
    /** Sets the program to be executed when run() is called. */
189
 
    void setProgram(const QString & program);
190
 
 
191
 
    /** Returns the session's current working directory. */
192
 
    QString initialWorkingDirectory() {
193
 
        return _initialWorkingDir;
194
 
    }
195
 
 
196
 
    /**
197
 
     * Sets the initial working directory for the session when it is run
198
 
     * This has no effect once the session has been started.
199
 
     */
200
 
    void setInitialWorkingDirectory( const QString & dir );
201
 
 
202
 
    /**
203
 
     * Sets the type of history store used by this session.
204
 
     * Lines of output produced by the terminal are added
205
 
     * to the history store.  The type of history store
206
 
     * used affects the number of lines which can be
207
 
     * remembered before they are lost and the storage
208
 
     * (in memory, on-disk etc.) used.
209
 
     */
210
 
    void setHistoryType(const HistoryType & type);
211
 
    /**
212
 
     * Returns the type of history store used by this session.
213
 
     */
214
 
    const HistoryType & historyType() const;
215
 
    /**
216
 
     * Clears the history store used by this session.
217
 
     */
218
 
    void clearHistory();
219
 
 
220
 
    /**
221
 
     * Enables monitoring for activity in the session.
222
 
     * This will cause notifySessionState() to be emitted
223
 
     * with the NOTIFYACTIVITY state flag when output is
224
 
     * received from the terminal.
225
 
     */
226
 
    void setMonitorActivity(bool);
227
 
    /** Returns true if monitoring for activity is enabled. */
228
 
    bool isMonitorActivity() const;
229
 
 
230
 
    /**
231
 
     * Enables monitoring for silence in the session.
232
 
     * This will cause notifySessionState() to be emitted
233
 
     * with the NOTIFYSILENCE state flag when output is not
234
 
     * received from the terminal for a certain period of
235
 
     * time, specified with setMonitorSilenceSeconds()
236
 
     */
237
 
    void setMonitorSilence(bool);
238
 
    /**
239
 
     * Returns true if monitoring for inactivity (silence)
240
 
     * in the session is enabled.
241
 
     */
242
 
    bool isMonitorSilence()  const;
243
 
    /** See setMonitorSilence() */
244
 
    void setMonitorSilenceSeconds(int seconds);
245
 
 
246
 
    /**
247
 
     * Sets the key bindings used by this session.  The bindings
248
 
     * specify how input key sequences are translated into
249
 
     * the character stream which is sent to the terminal.
250
 
     *
251
 
     * @param id The name of the key bindings to use.  The
252
 
     * names of available key bindings can be determined using the
253
 
     * KeyboardTranslatorManager class.
254
 
     */
255
 
    void setKeyBindings(const QString & id);
256
 
    /** Returns the name of the key bindings used by this session. */
257
 
    QString keyBindings() const;
258
 
 
259
 
    /**
260
 
     * This enum describes the available title roles.
261
 
     */
262
 
    enum TitleRole {
263
 
        /** The name of the session. */
264
 
        NameRole,
265
 
        /** The title of the session which is displayed in tabs etc. */
266
 
        DisplayedTitleRole
267
 
    };
268
 
 
269
 
    /** Sets the session's title for the specified @p role to @p title. */
270
 
    void setTitle(TitleRole role , const QString & title);
271
 
    /** Returns the session's title for the specified @p role. */
272
 
    QString title(TitleRole role) const;
273
 
    /** Convenience method used to read the name property.  Returns title(Session::NameRole). */
274
 
    QString nameTitle() const {
275
 
        return title(Session::NameRole);
276
 
    }
277
 
 
278
 
    /** Sets the name of the icon associated with this session. */
279
 
    void setIconName(const QString & iconName);
280
 
    /** Returns the name of the icon associated with this session. */
281
 
    QString iconName() const;
282
 
 
283
 
    /** Sets the text of the icon associated with this session. */
284
 
    void setIconText(const QString & iconText);
285
 
    /** Returns the text of the icon associated with this session. */
286
 
    QString iconText() const;
287
 
 
288
 
    /** Specifies whether a utmp entry should be created for the pty used by this session. */
289
 
    void setAddToUtmp(bool);
290
 
 
291
 
    /** Sends the specified @p signal to the terminal process. */
292
 
    bool sendSignal(int signal);
293
 
 
294
 
    /**
295
 
     * Specifies whether to close the session automatically when the terminal
296
 
     * process terminates.
297
 
     */
298
 
    void setAutoClose(bool b) {
299
 
        _autoClose = b;
300
 
    }
301
 
 
302
 
    /**
303
 
     * Sets whether flow control is enabled for this terminal
304
 
     * session.
305
 
     */
306
 
    void setFlowControlEnabled(bool enabled);
307
 
 
308
 
    /** Returns whether flow control is enabled for this terminal session. */
309
 
    bool flowControlEnabled() const;
310
 
 
311
 
    /**
312
 
     * Sends @p text to the current foreground terminal program.
313
 
     */
314
 
    void sendText(const QString &text) const;
315
 
 
316
 
    /**
317
 
     * Sends @p QKeyEvent to the current foreground terminal program.
318
 
     */
319
 
    void sendKey(QKeyEvent *key);
320
 
 
321
 
    /**
322
 
     * Returns the process id of the terminal process.
323
 
     * This is the id used by the system API to refer to the process.
324
 
     */
325
 
    int processId() const;
326
 
 
327
 
    /**
328
 
     * Returns the process id of the terminal's foreground process.
329
 
     * This is initially the same as processId() but can change
330
 
     * as the user starts other programs inside the terminal.
331
 
     */
332
 
    int foregroundProcessId() const;
333
 
 
334
 
    /** Returns the terminal session's window size in lines and columns. */
335
 
    QSize size();
336
 
    /**
337
 
     * Emits a request to resize the session to accommodate
338
 
     * the specified window size.
339
 
     *
340
 
     * @param size The size in lines and columns to request.
341
 
     */
342
 
    void setSize(const QSize & size);
343
 
 
344
 
    /** Sets the text codec used by this session's terminal emulation. */
345
 
    void setCodec(QTextCodec * codec);
346
 
 
347
 
    /**
348
 
     * Sets whether the session has a dark background or not.  The session
349
 
     * uses this information to set the COLORFGBG variable in the process's
350
 
     * environment, which allows the programs running in the terminal to determine
351
 
     * whether the background is light or dark and use appropriate colors by default.
352
 
     *
353
 
     * This has no effect once the session is running.
354
 
     */
355
 
    void setDarkBackground(bool darkBackground);
356
 
    /**
357
 
     * Returns true if the session has a dark background.
358
 
     * See setDarkBackground()
359
 
     */
360
 
    bool hasDarkBackground() const;
361
 
 
362
 
    /**
363
 
     * Attempts to get the shell program to redraw the current display area.
364
 
     * This can be used after clearing the screen, for example, to get the
365
 
     * shell to redraw the prompt line.
366
 
     */
367
 
    void refresh();
368
 
 
369
 
//  void startZModem(const QString &rz, const QString &dir, const QStringList &list);
370
 
//  void cancelZModem();
371
 
//  bool isZModemBusy() { return _zmodemBusy; }
372
 
 
373
 
public slots:
374
 
 
375
 
    /**
376
 
     * Starts the terminal session.
377
 
     *
378
 
     * This creates the terminal process and connects the teletype to it.
379
 
     */
380
 
    void run();
381
 
 
382
 
    /**
383
 
     * Closes the terminal session.  This sends a hangup signal
384
 
     * (SIGHUP) to the terminal process and causes the done(Session*)
385
 
     * signal to be emitted.
386
 
     */
387
 
    void close();
388
 
 
389
 
    /**
390
 
     * Changes the session title or other customizable aspects of the terminal
391
 
     * emulation display. For a list of what may be changed see the
392
 
     * Emulation::titleChanged() signal.
393
 
     */
394
 
    void setUserTitle( int, const QString & caption );
395
 
 
396
 
signals:
397
 
 
398
 
    /** Emitted when the terminal process starts. */
399
 
    void started();
400
 
 
401
 
    /**
402
 
     * Emitted when the terminal process exits.
403
 
     */
404
 
    void finished();
405
 
 
406
 
    /**
407
 
     * Emitted when output is received from the terminal process.
408
 
     */
409
 
    void receivedData( const QString & text );
410
 
 
411
 
    /** Emitted when the session's title has changed. */
412
 
    void titleChanged();
413
 
 
414
 
    /** Emitted when the session's profile has changed. */
415
 
    void profileChanged(const QString & profile);
416
 
 
417
 
    /**
418
 
     * Emitted when the activity state of this session changes.
419
 
     *
420
 
     * @param state The new state of the session.  This may be one
421
 
     * of NOTIFYNORMAL, NOTIFYSILENCE or NOTIFYACTIVITY
422
 
     */
423
 
    void stateChanged(int state);
424
 
 
425
 
    /** Emitted when a bell event occurs in the session. */
426
 
    void bellRequest( const QString & message );
427
 
 
428
 
    /**
429
 
     * Requests that the color the text for any tabs associated with
430
 
     * this session should be changed;
431
 
     *
432
 
     * TODO: Document what the parameter does
433
 
     */
434
 
    void changeTabTextColorRequest(int);
435
 
 
436
 
    /**
437
 
     * Requests that the background color of views on this session
438
 
     * should be changed.
439
 
     */
440
 
    void changeBackgroundColorRequest(const QColor &);
441
 
 
442
 
    /** TODO: Document me. */
443
 
    void openUrlRequest(const QString & url);
444
 
 
445
 
    /** TODO: Document me. */
446
 
//  void zmodemDetected();
447
 
 
448
 
    /**
449
 
     * Emitted when the terminal process requests a change
450
 
     * in the size of the terminal window.
451
 
     *
452
 
     * @param size The requested window size in terms of lines and columns.
453
 
     */
454
 
    void resizeRequest(const QSize & size);
455
 
 
456
 
    /**
457
 
     * Emitted when a profile change command is received from the terminal.
458
 
     *
459
 
     * @param text The text of the command.  This is a string of the form
460
 
     * "PropertyName=Value;PropertyName=Value ..."
461
 
     */
462
 
    void profileChangeCommandReceived(const QString & text);
463
 
 
464
 
    /**
465
 
     * Emitted when the flow control state changes.
466
 
     *
467
 
     * @param enabled True if flow control is enabled or false otherwise.
468
 
     */
469
 
    void flowControlEnabledChanged(bool enabled);
470
 
 
471
 
private slots:
472
 
    void done(int);
473
 
 
474
 
//  void fireZModemDetected();
475
 
 
476
 
    void onReceiveBlock( const char * buffer, int len );
477
 
    void monitorTimerDone();
478
 
 
479
 
    void onViewSizeChange(int height, int width);
480
 
    void onEmulationSizeChange(int lines , int columns);
481
 
 
482
 
    void activityStateSet(int);
483
 
 
484
 
    //automatically detach views from sessions when view is destroyed
485
 
    void viewDestroyed(QObject * view);
486
 
 
487
 
//  void zmodemReadStatus();
488
 
//  void zmodemReadAndSendBlock();
489
 
//  void zmodemRcvBlock(const char *data, int len);
490
 
//  void zmodemFinished();
491
 
 
492
 
private:
493
 
 
494
 
    void updateTerminalSize();
495
 
    WId windowId() const;
496
 
 
497
 
    int            _uniqueIdentifier;
498
 
 
499
 
    Pty     *_shellProcess;
500
 
    Emulation  *  _emulation;
501
 
 
502
 
    QList<KTerminalDisplay *> _views;
503
 
 
504
 
    bool           _monitorActivity;
505
 
    bool           _monitorSilence;
506
 
    bool           _notifiedActivity;
507
 
    bool           _masterMode;
508
 
    bool           _autoClose;
509
 
    bool           _wantedClose;
510
 
    QTimer    *    _monitorTimer;
511
 
 
512
 
    int            _silenceSeconds;
513
 
 
514
 
    QString        _nameTitle;
515
 
    QString        _displayTitle;
516
 
    QString        _userTitle;
517
 
 
518
 
    QString        _localTabTitleFormat;
519
 
    QString        _remoteTabTitleFormat;
520
 
 
521
 
    QString        _iconName;
522
 
    QString        _iconText; // as set by: echo -en '\033]1;IconText\007
523
 
    bool           _addToUtmp;
524
 
    bool           _flowControl;
525
 
    bool           _fullScripting;
526
 
 
527
 
    QString        _program;
528
 
    QStringList    _arguments;
529
 
 
530
 
    QStringList    _environment;
531
 
    int            _sessionId;
532
 
 
533
 
    QString        _initialWorkingDir;
534
 
 
535
 
    // ZModem
536
 
//  bool           _zmodemBusy;
537
 
//  KProcess*      _zmodemProc;
538
 
//  ZModemDialog*  _zmodemProgress;
539
 
 
540
 
    // Color/Font Changes by ESC Sequences
541
 
 
542
 
    QColor         _modifiedBackground; // as set by: echo -en '\033]11;Color\007
543
 
 
544
 
    QString        _profileKey;
545
 
 
546
 
    bool _hasDarkBackground;
547
 
 
548
 
    static int lastSessionId;
549
 
 
550
 
};
551
 
 
552
 
/**
553
 
 * Provides a group of sessions which is divided into master and slave sessions.
554
 
 * Activity in master sessions can be propagated to all sessions within the group.
555
 
 * The type of activity which is propagated and method of propagation is controlled
556
 
 * by the masterMode() flags.
557
 
 */
558
 
class SessionGroup : public QObject {
559
 
    Q_OBJECT
560
 
 
561
 
public:
562
 
    /** Constructs an empty session group. */
563
 
    SessionGroup();
564
 
    /** Destroys the session group and removes all connections between master and slave sessions. */
565
 
    ~SessionGroup();
566
 
 
567
 
    /** Adds a session to the group. */
568
 
    void addSession( Session * session );
569
 
    /** Removes a session from the group. */
570
 
    void removeSession( Session * session );
571
 
 
572
 
    /** Returns the list of sessions currently in the group. */
573
 
    QList<Session *> sessions() const;
574
 
 
575
 
    /**
576
 
     * Sets whether a particular session is a master within the group.
577
 
     * Changes or activity in the group's master sessions may be propagated
578
 
     * to all the sessions in the group, depending on the current masterMode()
579
 
     *
580
 
     * @param session The session whoose master status should be changed.
581
 
     * @param master True to make this session a master or false otherwise
582
 
     */
583
 
    void setMasterStatus( Session * session , bool master );
584
 
    /** Returns the master status of a session.  See setMasterStatus() */
585
 
    bool masterStatus( Session * session ) const;
586
 
 
587
 
    /**
588
 
     * This enum describes the options for propagating certain activity or
589
 
     * changes in the group's master sessions to all sessions in the group.
590
 
     */
591
 
    enum MasterMode {
592
 
        /**
593
 
         * Any input key presses in the master sessions are sent to all
594
 
         * sessions in the group.
595
 
         */
596
 
        CopyInputToAll = 1
597
 
    };
598
 
 
599
 
    /**
600
 
     * Specifies which activity in the group's master sessions is propagated
601
 
     * to all sessions in the group.
602
 
     *
603
 
     * @param mode A bitwise OR of MasterMode flags.
604
 
     */
605
 
    void setMasterMode( int mode );
606
 
    /**
607
 
     * Returns a bitwise OR of the active MasterMode flags for this group.
608
 
     * See setMasterMode()
609
 
     */
610
 
    int masterMode() const;
611
 
 
612
 
private:
613
 
    void connectPair(Session * master , Session * other);
614
 
    void disconnectPair(Session * master , Session * other);
615
 
    void connectAll(bool connect);
616
 
    QList<Session *> masters() const;
617
 
 
618
 
    // maps sessions to their master status
619
 
    QHash<Session *,bool> _sessions;
620
 
 
621
 
    int _masterMode;
622
 
};
623
 
 
624
 
 
625
 
 
626
 
#endif