Session.h 27.5 KB
Newer Older
1 2
/*
    This file is part of Konsole, an X terminal.
3

4 5
    Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
    Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
6
    Copyright 2009 by Thomas Dreibholz <dreibh@iem.uni-due.de>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/
23 24 25 26

#ifndef SESSION_H
#define SESSION_H

27
// Qt
28 29 30 31 32
#include <QStringList>
#include <QHash>
#include <QUuid>
#include <QSize>
#include <QProcess>
33
#include <QWidget>
34
#include <QUrl>
35

36
// Konsole
Laurent Montel's avatar
Laurent Montel committed
37
#include "konsoleprivate_export.h"
38
#include "config-konsole.h"
39
#include "Shortcut_p.h"
40

41 42
class QColor;

43 44
class KConfigGroup;
class KProcess;
Robert Knight's avatar
 
Robert Knight committed
45

Kurt Hindenburg's avatar
Kurt Hindenburg committed
46
namespace Konsole {
47 48
class Emulation;
class Pty;
Robert Knight's avatar
 
Robert Knight committed
49
class ProcessInfo;
50 51
class TerminalDisplay;
class ZModemDialog;
52
class HistoryType;
Waldo Bastian's avatar
Waldo Bastian committed
53

Robert Knight's avatar
 
Robert Knight committed
54
/**
55 56
 * Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
 * The pseudo-teletype (or PTY) handles I/O between the terminal process and Konsole.
57 58
 * The terminal emulation ( Emulation and subclasses ) processes the output stream from the
 * PTY and produces a character image which is then shown on views connected to the session.
Robert Knight's avatar
 
Robert Knight committed
59
 *
60
 * Each Session can be connected to one or more views by using the addView() method.
Robert Knight's avatar
 
Robert Knight committed
61 62 63 64
 * The attached views can then display output from the program running in the terminal
 * or send input to the program in the terminal in the form of keypresses and mouse
 * activity.
 */
65
class KONSOLEPRIVATE_EXPORT Session : public QObject
66
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
67 68
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "org.kde.konsole.Session")
69 70

public:
Kurt Hindenburg's avatar
Kurt Hindenburg committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    Q_PROPERTY(QString name READ nameTitle)
    Q_PROPERTY(int processId READ processId)
    Q_PROPERTY(QString keyBindings READ keyBindings WRITE setKeyBindings)
    Q_PROPERTY(QSize size READ size WRITE setSize)

    /**
     * Constructs a new session.
     *
     * To start the terminal process, call the run() method,
     * after specifying the program and arguments
     * using setProgram() and setArguments()
     *
     * If no program or arguments are specified explicitly, the Session
     * falls back to using the program specified in the SHELL environment
     * variable.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
87
    explicit Session(QObject *parent = nullptr);
88
    ~Session() override;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
89 90 91 92 93 94 95 96 97

    /**
     * Connect to an existing terminal.  When a new Session() is constructed it
     * automatically searches for and opens a new teletype.  If you want to
     * use an existing teletype (given its file descriptor) call this after
     * constructing the session.
     *
     * Calling openTeletype() while a session is running has no effect.
     *
98
     * @param fd The file descriptor of the pseudo-teletype master (See KPtyProcess::KPtyProcess())
99 100
     * @param runShell When true, runs the teletype in a shell session environment.
     * When false, the session is not run, so that the KPtyProcess can be standalone.
Kurt Hindenburg's avatar
Kurt Hindenburg committed
101
     */
102
    void openTeletype(int fd, bool runShell);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
103 104 105 106 107 108 109

    /**
     * Returns true if the session is currently running.  This will be true
     * after run() has been called successfully.
     */
    bool isRunning() const;

110 111 112 113 114 115
    /**
     * Returns true if the tab holding this session is currently selected
     * and Konsole is the foreground window.
     */
    bool hasFocus() const;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
116 117 118 119 120 121 122 123 124 125
    /**
     * Adds a new view for this session.
     *
     * The viewing widget will display the output from the terminal and
     * input from the viewing widget (key presses, mouse activity etc.)
     * will be sent to the terminal.
     *
     * Views can be removed using removeView().  The session is automatically
     * closed when the last view is removed.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
126
    void addView(TerminalDisplay *widget);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
127 128 129 130 131 132 133
    /**
     * Removes a view from this session.  When the last view is removed,
     * the session will be closed automatically.
     *
     * @p widget will no longer display output from or send input
     * to the terminal
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
134
    void removeView(TerminalDisplay *widget);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
135 136 137 138

    /**
     * Returns the views connected to this session
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
139
    QList<TerminalDisplay *> views() const;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
140 141 142 143 144

    /**
     * Returns the terminal emulation instance being used to encode / decode
     * characters to / from the process.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
145
    Emulation *emulation() const;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

    /** Returns the unique ID for this session. */
    int sessionId() const;

    /**
     * This enum describes the contexts for which separate
     * tab title formats may be specified.
     */
    enum TabTitleContext {
        /** Default tab title format */
        LocalTabTitle,
        /**
         * Tab title format used session currently contains
         * a connection to a remote computer (via SSH)
         */
        RemoteTabTitle
    };

    /**
     * Returns true if the session currently contains a connection to a
     * remote computer.  It currently supports ssh.
     */
    bool isRemote();

    /**
     * Sets the format used by this session for tab titles.
     *
     * @param context The context whose format should be set.
     * @param format The tab title format.  This may be a mixture
     * of plain text and dynamic elements denoted by a '%' character
     * followed by a letter.  (eg. %d for directory).  The dynamic
     * elements available depend on the @p context
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
179
    void setTabTitleFormat(TabTitleContext context, const QString &format);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
180 181 182
    /** Returns the format used by this session for tab titles. */
    QString tabTitleFormat(TabTitleContext context) const;

183 184 185 186 187 188 189 190 191
    /**
     * Sets the color user by this session for tab.
     * 
     * @param color The background color for the tab.
     */
    void setColor(const QColor &color);
    /** Returns the color used by this session for tab. */
    QColor color() const;

192 193 194 195 196 197
    /**
     * Returns true if the tab title has been changed by the user via the
     * rename-tab dialog.
     */
    bool isTabTitleSetByUser() const;

198 199 200 201 202 203
    /**
     * Returns true if the tab color has been changed by the user via the
     * rename-tab dialog.
     */
    bool isTabColorSetByUser() const;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
204 205 206 207 208 209 210 211 212
    /** Returns the arguments passed to the shell process when run() is called. */
    QStringList arguments() const;
    /** Returns the program name of the shell process started when run() is called. */
    QString program() const;

    /**
     * Sets the command line arguments which the session's program will be passed when
     * run() is called.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
213
    void setArguments(const QStringList &arguments);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
214
    /** Sets the program to be executed when run() is called. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
215
    void setProgram(const QString &program);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
216 217

    /** Returns the session's current working directory. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
218 219
    QString initialWorkingDirectory()
    {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
220 221 222 223 224 225 226
        return _initialWorkingDir;
    }

    /**
     * Sets the initial working directory for the session when it is run
     * This has no effect once the session has been started.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
227
    void setInitialWorkingDirectory(const QString &dir);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241

    /**
     * Returns the current directory of the foreground process in the session
     */
    QString currentWorkingDirectory();

    /**
     * Sets the type of history store used by this session.
     * Lines of output produced by the terminal are added
     * to the history store.  The type of history store
     * used affects the number of lines which can be
     * remembered before they are lost and the storage
     * (in memory, on-disk etc.) used.
     */
242
    void setHistoryType(const HistoryType &hType);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
243 244 245
    /**
     * Returns the type of history store used by this session.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
246
    const HistoryType &historyType() const;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
247 248 249 250 251 252 253 254 255 256
    /**
     * Clears the history store used by this session.
     */
    void clearHistory();

    /**
     * Sets the key bindings used by this session.  The bindings
     * specify how input key sequences are translated into
     * the character stream which is sent to the terminal.
     *
Kurt Hindenburg's avatar
Kurt Hindenburg committed
257
     * @param name The name of the key bindings to use.  The
Kurt Hindenburg's avatar
Kurt Hindenburg committed
258 259 260
     * names of available key bindings can be determined using the
     * KeyboardTranslatorManager class.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
261
    void setKeyBindings(const QString &name);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    /** Returns the name of the key bindings used by this session. */
    QString keyBindings() const;

    /**
     * This enum describes the available title roles.
     */
    enum TitleRole {
        /** The name of the session. */
        NameRole,
        /** The title of the session which is displayed in tabs etc. */
        DisplayedTitleRole
    };

    /**
     * Return the session title set by the user (ie. the program running
     * in the terminal), or an empty string if the user has not set a custom title
     */
    QString userTitle() const;

    /** Convenience method used to read the name property.  Returns title(Session::NameRole). */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
282 283
    QString nameTitle() const
    {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
284 285
        return title(Session::NameRole);
    }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
286

Kurt Hindenburg's avatar
Kurt Hindenburg committed
287 288 289 290
    /** Returns a title generated from tab format and process information. */
    QString getDynamicTitle();

    /** Sets the name of the icon associated with this session. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
291
    void setIconName(const QString &iconName);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
292 293 294 295
    /** Returns the name of the icon associated with this session. */
    QString iconName() const;

    /** Return URL for the session. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
296
    QUrl getUrl();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
297 298

    /** Sets the text of the icon associated with this session. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
299
    void setIconText(const QString &iconText);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
300 301 302 303
    /** Returns the text of the icon associated with this session. */
    QString iconText() const;

    /** Sets the session's title for the specified @p role to @p title. */
304
    void setTitle(TitleRole role, const QString &newTitle);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318

    /** Returns the session's title for the specified @p role. */
    QString title(TitleRole role) const;

    /**
     * Specifies whether a utmp entry should be created for the pty used by this session.
     * If true, KPty::login() is called when the session is started.
     */
    void setAddToUtmp(bool);

    /**
     * Specifies whether to close the session automatically when the terminal
     * process terminates.
     */
319 320 321 322
    void setAutoClose(bool close);

    /** See setAutoClose() */
    bool autoClose() const;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

    /** Returns true if the user has started a program in the session. */
    bool isForegroundProcessActive();

    /** Returns the name of the current foreground process. */
    QString foregroundProcessName();

    /** Returns the terminal session's window size in lines and columns. */
    QSize size();
    /**
     * Emits a request to resize the session to accommodate
     * the specified window size.
     *
     * @param size The size in lines and columns to request.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
338
    void setSize(const QSize &size);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
339

340 341
    QSize preferredSize() const;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
342
    void setPreferredSize(const QSize &size);
343

Kurt Hindenburg's avatar
Kurt Hindenburg committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
    /**
     * Sets whether the session has a dark background or not.  The session
     * uses this information to set the COLORFGBG variable in the process's
     * environment, which allows the programs running in the terminal to determine
     * whether the background is light or dark and use appropriate colors by default.
     *
     * This has no effect once the session is running.
     */
    void setDarkBackground(bool darkBackground);

    /**
     * Attempts to get the shell program to redraw the current display area.
     * This can be used after clearing the screen, for example, to get the
     * shell to redraw the prompt line.
     */
    void refresh();

361
    void startZModem(const QString &zmodem, const QString &dir, const QStringList &list);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
362
    void cancelZModem();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
363 364
    bool isZModemBusy()
    {
Kurt Hindenburg's avatar
Kurt Hindenburg committed
365 366
        return _zmodemBusy;
    }
367 368 369 370
    void setZModemBusy(bool busy)
    {
        _zmodemBusy = busy;
    }
Kurt Hindenburg's avatar
Kurt Hindenburg committed
371 372

    /**
373 374
      * Possible values of the @p what parameter for setSessionAttribute().
      * See the "Operating System Commands" section at:
375
      * https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands
Kurt Hindenburg's avatar
Kurt Hindenburg committed
376
      */
377
    enum SessionAttributes {
378
        IconNameAndWindowTitle = 0,
Kurt Hindenburg's avatar
Kurt Hindenburg committed
379 380 381 382 383 384 385 386
        IconName = 1,
        WindowTitle = 2,
        CurrentDirectory = 7,         // From VTE (supposedly 6 was for dir, 7 for file, but whatever)
        TextColor = 10,
        BackgroundColor = 11,
        SessionName = 30,             // Non-standard
        SessionIcon = 32,             // Non-standard
        ProfileChange = 50            // this clashes with Xterm's font change command
Kurt Hindenburg's avatar
Kurt Hindenburg committed
387 388 389
    };

    // Sets the text codec used by this sessions terminal emulation.
Kurt Hindenburg's avatar
Kurt Hindenburg committed
390
    void setCodec(QTextCodec *codec);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
391 392

    // session management
Kurt Hindenburg's avatar
Kurt Hindenburg committed
393 394
    void saveSession(KConfigGroup &group);
    void restoreSession(KConfigGroup &group);
Robert Knight's avatar
 
Robert Knight committed
395

396 397
    void sendSignal(int signal);

398 399
    void reportColor(SessionAttributes r, const QColor &c);
    void reportForegroundColor(const QColor &c);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
400
    void reportBackgroundColor(const QColor &c);
401

Kurt Hindenburg's avatar
Kurt Hindenburg committed
402 403 404
    bool isReadOnly() const;
    void setReadOnly(bool readOnly);

405 406 407
    // Returns true if the current screen is the secondary/alternate one
    // or false if it's the primary/normal buffer
    bool isPrimaryScreen();
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
408
    void tabTitleSetByUser(bool set);
409
    void tabColorSetByUser(bool set);
410

411 412 413 414 415 416 417 418 419 420 421
    enum Notification {
        NoNotification = 0,
        Activity = 1,
        Silence = 2,
        Bell = 4,
    };
    Q_DECLARE_FLAGS(Notifications, Notification)

    /** Returns active notifications. */
    Notifications activeNotifications() const { return _activeNotifications; }

422
public Q_SLOTS:
423

Kurt Hindenburg's avatar
Kurt Hindenburg committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
    /**
     * Starts the terminal session.
     *
     * This creates the terminal process and connects the teletype to it.
     */
    void run();

    /**
     * Returns the environment of this session as a list of strings like
     * VARIABLE=VALUE
     */
    Q_SCRIPTABLE QStringList environment() const;

    /**
     * Sets the environment for this session.
     * @p environment should be a list of strings like
     * VARIABLE=VALUE
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
442
    Q_SCRIPTABLE void setEnvironment(const QStringList &environment);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
443

Jekyll Wu's avatar
Jekyll Wu committed
444 445 446 447
    /**
     * Adds one entry for the environment of this session
     * @p entry should be like VARIABLE=VALUE
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
448
    void addEnvironmentEntry(const QString &entry);
449

Kurt Hindenburg's avatar
Kurt Hindenburg committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    /**
     * Closes the terminal session. It kills the terminal process by calling
     * closeInNormalWay() and, optionally, closeInForceWay().
     */
    //Q_SCRIPTABLE void close(); // This cause the menu issues bko 185466
    void close();

    /**
     * Kill the terminal process in normal way. This sends a hangup signal
     * (SIGHUP) to the terminal process and causes the finished() signal to
     * be emitted. If the process does not respond to the SIGHUP signal then
     * the terminal connection (the pty) is closed and Konsole waits for the
     * process to exit. This method works most of the time, but fails with some
     * programs which respond to SIGHUP signal in special way, such as autossh
     * and irssi.
     */
    bool closeInNormalWay();

    /**
     * kill terminal process in force way. This send a SIGKILL signal to the
     * terminal process. It should be called only after closeInNormalWay() has
     * failed. Take it as last resort.
     */
    bool closeInForceWay();

    /**
476 477 478
     * Changes one of certain session attributes in the terminal emulation
     * display. For a list of what may be changed see the
     * Emulation::sessionAttributeChanged() signal.
Kurt Hindenburg's avatar
Kurt Hindenburg committed
479
     *
480 481
     * @param what The session attribute being changed, it is one of the
     * SessionAttributes enum
Kurt Hindenburg's avatar
Kurt Hindenburg committed
482 483
     * @param caption The text part of the terminal command
     */
484
    void setSessionAttribute(int what, const QString &caption);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

    /**
     * Enables monitoring for activity in the session.
     * This will cause notifySessionState() to be emitted
     * with the NOTIFYACTIVITY state flag when output is
     * received from the terminal.
     */
    Q_SCRIPTABLE void setMonitorActivity(bool);

    /** Returns true if monitoring for activity is enabled. */
    Q_SCRIPTABLE bool isMonitorActivity() const;

    /**
     * Enables monitoring for silence in the session.
     * This will cause notifySessionState() to be emitted
     * with the NOTIFYSILENCE state flag when output is not
     * received from the terminal for a certain period of
     * time, specified with setMonitorSilenceSeconds()
     */
    Q_SCRIPTABLE void setMonitorSilence(bool);

    /**
     * Returns true if monitoring for inactivity (silence)
     * in the session is enabled.
     */
    Q_SCRIPTABLE bool isMonitorSilence() const;

    /** See setMonitorSilence() */
    Q_SCRIPTABLE void setMonitorSilenceSeconds(int seconds);

    /**
     * Sets whether flow control is enabled for this terminal
     * session.
     */
    Q_SCRIPTABLE void setFlowControlEnabled(bool enabled);

    /** Returns whether flow control is enabled for this terminal session. */
    Q_SCRIPTABLE bool flowControlEnabled() const;

    /**
Kurt Hindenburg's avatar
Kurt Hindenburg committed
525
     * @param text to send to the current foreground terminal program.
526
     * @param eol send this after @p text
Kurt Hindenburg's avatar
Kurt Hindenburg committed
527
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
528
    void sendTextToTerminal(const QString &text, const QChar &eol = QChar()) const;
529

530
#if defined(REMOVE_SENDTEXT_RUNCOMMAND_DBUS_METHODS)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
531
    void sendText(const QString &text) const;
532
#else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
533
    Q_SCRIPTABLE void sendText(const QString &text) const;
534
#endif
Kurt Hindenburg's avatar
Kurt Hindenburg committed
535

536 537 538
    /**
     * Sends @p command to the current foreground terminal program.
     */
539
#if defined(REMOVE_SENDTEXT_RUNCOMMAND_DBUS_METHODS)
Kurt Hindenburg's avatar
Kurt Hindenburg committed
540
    void runCommand(const QString &command) const;
541
#else
Kurt Hindenburg's avatar
Kurt Hindenburg committed
542
    Q_SCRIPTABLE void runCommand(const QString &command) const;
543
#endif
544

Kurt Hindenburg's avatar
Kurt Hindenburg committed
545 546 547 548 549 550 551 552 553 554
    /**
     * Sends a mouse event of type @p eventType emitted by button
     * @p buttons on @p column/@p line to the current foreground
     * terminal program
     */
    Q_SCRIPTABLE void sendMouseEvent(int buttons, int column, int line, int eventType);

    /**
    * Returns the process id of the terminal process.
    * This is the id used by the system API to refer to the process.
555
    */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
556 557 558 559 560 561 562 563 564 565 566
    Q_SCRIPTABLE int processId() const;

    /**
     * Returns the process id of the terminal's foreground process.
     * This is initially the same as processId() but can change
     * as the user starts other programs inside the terminal.
     */
    Q_SCRIPTABLE int foregroundProcessId();

    /** Sets the text codec used by this sessions terminal emulation.
      * Overloaded to accept a QByteArray for convenience since DBus
567
      * does not accept QTextCodec directly.
Kurt Hindenburg's avatar
Kurt Hindenburg committed
568
      */
569
    Q_SCRIPTABLE bool setCodec(const QByteArray &name);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
570 571 572 573 574 575 576 577 578 579 580

    /** Returns the codec used to decode incoming characters in this
     * terminal emulation
     */
    Q_SCRIPTABLE QByteArray codec();

    /** Sets the session's title for the specified @p role to @p title.
     *  This is an overloaded member function for setTitle(TitleRole, QString)
     *  provided for convenience since enum data types may not be
     *  exported directly through DBus
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
581
    Q_SCRIPTABLE void setTitle(int role, const QString &title);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
582 583 584 585 586 587 588 589 590 591 592 593

    /** Returns the session's title for the specified @p role.
     * This is an overloaded member function for  setTitle(TitleRole)
     * provided for convenience since enum data types may not be
     * exported directly through DBus
     */
    Q_SCRIPTABLE QString title(int role) const;

    /** Returns the "friendly" version of the QUuid of this session.
    * This is a QUuid with the braces and dashes removed, so it cannot be
    * used to construct a new QUuid. The same text appears in the
    * SHELL_SESSION_ID environment variable.
594
    */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
595 596 597 598 599 600 601
    Q_SCRIPTABLE QString shellSessionId() const;

    /** Sets the session's tab title format for the specified @p context to @p format.
     *  This is an overloaded member function for setTabTitleFormat(TabTitleContext, QString)
     *  provided for convenience since enum data types may not be
     *  exported directly through DBus
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
602
    Q_SCRIPTABLE void setTabTitleFormat(int context, const QString &format);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
603 604 605 606 607 608 609

    /** Returns the session's tab title format for the specified @p context.
     * This is an overloaded member function for tabTitleFormat(TitleRole)
     * provided for convenience since enum data types may not be
     * exported directly through DBus
     */
    Q_SCRIPTABLE QString tabTitleFormat(int context) const;
610

611 612 613
    /**
     * Sets the history capacity of this session.
     *
Kurt Hindenburg's avatar
Kurt Hindenburg committed
614
     * @param lines The history capacity in unit of lines. Its value can be:
615 616 617 618 619 620
     * <ul>
     * <li> positive integer  -  fixed size history</li>
     * <li> 0 -  no history</li>
     * <li> negative integer -  unlimited history</li>
     * </ul>
     */
621
    Q_SCRIPTABLE void setHistorySize(int lines);
622 623 624 625 626 627

    /**
     * Returns the history capacity of this session.
     */
    Q_SCRIPTABLE int historySize() const;

628 629 630
    /**
     * Sets the current session's profile
     */
631
    Q_SCRIPTABLE void setProfile(const QString &profileName);
632

633 634 635 636 637
    /**
     * Returns the current session's profile name
     */
    Q_SCRIPTABLE QString profile();

638
Q_SIGNALS:
639

Kurt Hindenburg's avatar
Kurt Hindenburg committed
640 641 642 643 644 645 646 647
    /** Emitted when the terminal process starts. */
    void started();

    /**
     * Emitted when the terminal process exits.
     */
    void finished();

648 649 650 651 652
    /**
     * Emitted when one of certain session attributes has been changed.
     * See setSessionAttribute().
     */
    void sessionAttributeChanged();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
653

654 655 656
    /** Emitted when the session gets locked / unlocked. */
    void readOnlyChanged();

Kurt Hindenburg's avatar
Kurt Hindenburg committed
657 658 659
    /**
     * Emitted when the current working directory of this session changes.
     *
660
     * @param dir The new current working directory of the session.
Kurt Hindenburg's avatar
Kurt Hindenburg committed
661
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
662
    void currentDirectoryChanged(const QString &dir);
663

664 665 666 667 668
    /**
     * Emitted when the session text encoding changes.
     */
    void sessionCodecChanged(QTextCodec *codec);

Kurt Hindenburg's avatar
Kurt Hindenburg committed
669
    /** Emitted when a bell event occurs in the session. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
670
    void bellRequest(const QString &message);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
671

672 673
    /** Emitted when @p notification state changed to @p enabled */
    void notificationsChanged(Notification notification, bool enabled);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
674 675 676 677 678

    /**
     * Requests that the background color of views on this session
     * should be changed.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
679
    void changeBackgroundColorRequest(const QColor &);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
680 681 682 683
    /**
     * Requests that the text color of views on this session should
     * be changed to @p color.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
684
    void changeForegroundColorRequest(const QColor &);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
685 686

    /** TODO: Document me. */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
687
    void openUrlRequest(const QString &url);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
688 689 690 691 692

    /**
     * Emitted when the request for data transmission through ZModem
     * protocol is detected.
     */
693 694
    void zmodemDownloadDetected();
    void zmodemUploadDetected();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
695 696 697 698 699 700 701

    /**
     * Emitted when the terminal process requests a change
     * in the size of the terminal window.
     *
     * @param size The requested window size in terms of lines and columns.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
702
    void resizeRequest(const QSize &size);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
703 704 705 706 707 708 709

    /**
     * Emitted when a profile change command is received from the terminal.
     *
     * @param text The text of the command.  This is a string of the form
     * "PropertyName=Value;PropertyName=Value ..."
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
710
    void profileChangeCommandReceived(const QString &text);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
711 712 713 714 715 716 717 718 719

    /**
     * Emitted when the flow control state changes.
     *
     * @param enabled True if flow control is enabled or false otherwise.
     */
    void flowControlEnabledChanged(bool enabled);

    /**
720
     * Emitted when the active screen is switched, to indicate whether the primary
Kurt Hindenburg's avatar
Kurt Hindenburg committed
721 722 723 724 725 726 727 728 729 730 731 732 733
     * screen is in use.
     *
     * This signal serves as a relayer of Emulation::priamyScreenInUse(bool),
     * making it usable for higher level component.
     */
    void primaryScreenInUse(bool use);

    /**
     * Emitted when the text selection is changed.
     *
     * This signal serves as a relayer of Emulation::selectedText(QString),
     * making it usable for higher level component.
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
734
    void selectionChanged(const QString &text);
735

736 737 738 739 740 741
    /**
     * Emitted when foreground request ("\033]10;?\a") terminal code received.
     * Terminal is expected send "\033]10;rgb:RRRR/GGGG/BBBB\a" response.
     */
    void getForegroundColor();

742
    /**
Kurt Hindenburg's avatar
Kurt Hindenburg committed
743 744
     * Emitted when background request ("\033]11;?\a") terminal code received.
     * Terminal is expected send "\033]11;rgb:RRRR/GGGG/BBBB\a" response.
745 746
     *
     * Originally implemented to support vim's background detection feature
Felix Yan's avatar
Felix Yan committed
747
     * (without explicitly setting 'bg=dark' within local/remote vimrc)
748 749 750
     */
    void getBackgroundColor();

751
private Q_SLOTS:
Kurt Hindenburg's avatar
Kurt Hindenburg committed
752
    void done(int, QProcess::ExitStatus);
753

754 755
    void fireZModemDownloadDetected();
    void fireZModemUploadDetected();
756

757
    void onReceiveBlock(const char *buf, int len);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
758 759
    void silenceTimerDone();
    void activityTimerDone();
760
    void resetNotifications();
761

Kurt Hindenburg's avatar
Kurt Hindenburg committed
762
    void onViewSizeChange(int height, int width);
763

Kurt Hindenburg's avatar
Kurt Hindenburg committed
764
    //automatically detach views from sessions when view is destroyed
Kurt Hindenburg's avatar
Kurt Hindenburg committed
765
    void viewDestroyed(QObject *view);
766

Kurt Hindenburg's avatar
Kurt Hindenburg committed
767 768
    void zmodemReadStatus();
    void zmodemReadAndSendBlock();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
769
    void zmodemReceiveBlock(const char *data, int len);
Kurt Hindenburg's avatar
Kurt Hindenburg committed
770
    void zmodemFinished();
771

Kurt Hindenburg's avatar
Kurt Hindenburg committed
772 773
    void updateFlowControlState(bool suspended);
    void updateWindowSize(int lines, int columns);
774

775
    // Relays the signal from Emulation and sets _isPrimaryScreen
Kurt Hindenburg's avatar
Kurt Hindenburg committed
776
    void onPrimaryScreenInUse(bool use);
777

778 779
    void sessionAttributeRequest(int id);

780 781 782 783 784 785 786 787
    /**
     * Requests that the color the text for any tabs associated with
     * this session should be changed;
     *
     * TODO: Document what the parameter does
     */
    void changeTabTextColor(int);

788
private:
789 790
    Q_DISABLE_COPY(Session)

791 792
    // checks that the binary 'program' is available and can be executed
    // returns the binary name if available or an empty string otherwise
Kurt Hindenburg's avatar
Kurt Hindenburg committed
793
    static QString checkProgram(const QString &program);
794

Kurt Hindenburg's avatar
Kurt Hindenburg committed
795 796 797 798 799 800
    void updateTerminalSize();
    WId windowId() const;
    bool kill(int signal);
    // print a warning message in the terminal.  This is used
    // if the program fails to start, or if the shell exits in
    // an unsuccessful manner
Kurt Hindenburg's avatar
Kurt Hindenburg committed
801 802
    void terminalWarning(const QString &message);
    ProcessInfo *getProcessInfo();
Kurt Hindenburg's avatar
Kurt Hindenburg committed
803 804
    void updateSessionProcessInfo();
    bool updateForegroundProcessInfo();
805
    void updateWorkingDirectory();
806

807
    QString validDirectory(const QString &dir) const;
808

Kurt Hindenburg's avatar
Kurt Hindenburg committed
809
    QUuid _uniqueIdentifier;            // SHELL_SESSION_ID
810

Kurt Hindenburg's avatar
Kurt Hindenburg committed
811 812
    Pty *_shellProcess;
    Emulation *_emulation;
813

Kurt Hindenburg's avatar
Kurt Hindenburg committed
814
    QList<TerminalDisplay *> _views;
815

Kurt Hindenburg's avatar
Kurt Hindenburg committed
816
    // monitor activity & silence
Kurt Hindenburg's avatar
Kurt Hindenburg committed
817 818 819 820 821 822
    bool _monitorActivity;
    bool _monitorSilence;
    bool _notifiedActivity;
    int _silenceSeconds;
    QTimer *_silenceTimer;
    QTimer *_activityTimer;
823

824 825 826 827 828
    void setPendingNotification(Notification notification, bool enable = true);
    void handleActivity();

    Notifications _activeNotifications;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
829 830
    bool _autoClose;
    bool _closePerUserRequest;
831

Kurt Hindenburg's avatar
Kurt Hindenburg committed
832 833 834
    QString _nameTitle;
    QString _displayTitle;
    QString _userTitle;
835

Kurt Hindenburg's avatar
Kurt Hindenburg committed
836 837
    QString _localTabTitleFormat;
    QString _remoteTabTitleFormat;
838
    QColor _tabColor;
839

840
    bool _tabTitleSetByUser;
841
    bool _tabColorSetByUser;
842

Kurt Hindenburg's avatar
Kurt Hindenburg committed
843 844 845 846
    QString _iconName;
    QString _iconText;        // not actually used
    bool _addToUtmp;
    bool _flowControlEnabled;
847

Kurt Hindenburg's avatar
Kurt Hindenburg committed
848 849
    QString _program;
    QStringList _arguments;
850

Kurt Hindenburg's avatar
Kurt Hindenburg committed
851 852
    QStringList _environment;
    int _sessionId;
853

Kurt Hindenburg's avatar
Kurt Hindenburg committed
854 855 856
    QString _initialWorkingDir;
    QString _currentWorkingDir;
    QUrl _reportedWorkingUrl;
Robert Knight's avatar
 
Robert Knight committed
857

Kurt Hindenburg's avatar
Kurt Hindenburg committed
858 859 860
    ProcessInfo *_sessionProcessInfo;
    ProcessInfo *_foregroundProcessInfo;
    int _foregroundPid;
Laurent Montel's avatar
Laurent Montel committed
861

Kurt Hindenburg's avatar
Kurt Hindenburg committed
862
    // ZModem
Kurt Hindenburg's avatar
Kurt Hindenburg committed
863 864 865
    bool _zmodemBusy;
    KProcess *_zmodemProc;
    ZModemDialog *_zmodemProgress;
866

Kurt Hindenburg's avatar
Kurt Hindenburg committed
867
    bool _hasDarkBackground;
868

869 870
    QSize _preferredSize;

Kurt Hindenburg's avatar
Kurt Hindenburg committed
871
    bool _readOnly;
Kurt Hindenburg's avatar
Kurt Hindenburg committed
872
    static int lastSessionId;
873 874

    bool _isPrimaryScreen;
875 876
};

Stephan Binner's avatar
Stephan Binner committed
877
}
878

879
#endif