1 /*
2     SPDX-License-Identifier: GPL-2.0-or-later
3 
4     SPDX-FileCopyrightText: 2002 Dario Abatianni <eisfuchs@tigress.com>
5     SPDX-FileCopyrightText: 2006-2008 Eike Hein <hein@kde.org>
6 */
7 
8 #ifndef CHATWINDOW_H
9 #define CHATWINDOW_H
10 
11 #include "identity.h"
12 #include "common.h"
13 
14 #include <QFile>
15 #include <QWidget>
16 #include <QVBoxLayout>
17 
18 
19 class IRCView;
20 class IRCInput;
21 class Server;
22 
23 class ChatWindow : public QWidget
24 {
25     Q_OBJECT
26 
27     public:
28         explicit ChatWindow(QWidget* parent);
29         ~ChatWindow() override;
30 
31         enum WindowType
32         {
33             Status=0,
34             Channel,
35             Query,
36             DccChat,
37             DccTransferPanel,
38             RawLog,
39             Notice,
40             SNotice,
41             ChannelList,
42             Konsole,
43             UrlCatcher,
44             NicksOnline,
45             LogFileReader
46         };
47 
48         /** Clean up and close this tab.  Return false if you want to cancel the close. */
49         virtual bool closeYourself(bool askForConfirmation = true);
50 
51         virtual void cycle();
52 
53         /** This should be called and set with a non-null server as soon
54          *  as possibly after ChatWindow is created.
55          *  @param newServer The server to set it to.
56          */
57         virtual void setServer(Server* newServer);
58         /** This should be called if setServer is not called - e.g.
59          *  in the case of konsolepanel.  This should be set as soon
60          *  as possible after creation.
61          */
62 
63         /** Get the server this is linked to.
64          *  @return The server it is associated with, or null if none.
65          */
66         Server* getServer() const;
67         void setTextView(IRCView* newView);
68         IRCView* getTextView() const;
setInputBar(IRCInput * newInputBar)69         void setInputBar(IRCInput* newInputBar) { m_inputBar = newInputBar; }
getInputBar()70         IRCInput* getInputBar() const { return m_inputBar; }
71         virtual bool log() const;
72 
73         QString getName() const;
74         QString getTitle() const;
75         QString getURI(bool passNetwork = true);
76 
77         void setType(WindowType newType);
78         WindowType getType() const;
79         virtual bool isTopLevelView() const;
80 
sendText(const QString &)81         virtual void sendText(const QString& /*text*/) {}
82 
83         virtual void append(const QString& nickname,const QString& message, const QHash<QString, QString> &messageTags, const QString& label = QString());
84         virtual void appendRaw(const QString& message, bool self = false);
85         virtual void appendLog(const QString& message);
86         virtual void appendQuery(const QString& nickname,const QString& message, const QHash<QString, QString> &messageTags = QHash<QString, QString>(), bool inChannel = false);
87         virtual void appendAction(const QString& nickname,const QString& message, const QHash<QString, QString> &messageTags = QHash<QString, QString>());
88         virtual void appendServerMessage(const QString& type,const QString& message, const QHash<QString, QString> &messageTags = QHash<QString, QString>(), bool parseURL = true);
89         virtual void appendCommandMessage(const QString& command, const QString& message, const QHash<QString, QString> &messageTags = QHash<QString, QString>(),
90             bool parseURL = true, bool self = false);
91         virtual void appendBacklogMessage(const QString& firstColumn,const QString& message);
92 
93         void clear();
94 
95         virtual QString getTextInLine() const;
96         /** Reimplement this to return true in all classes that /can/ become front view.
97          */
98         virtual bool canBeFrontView() const;
99 
100         /** Reimplement this to return true in all classes that you can search in - i.e. use "Edit->Find Text" in.
101          */
102         virtual bool searchView() const;
103 
notificationsEnabled()104         bool notificationsEnabled() const { return m_notificationsEnabled; }
105 
106         bool eventFilter(QObject* watched, QEvent* e) override;
107 
logFileName()108         QString logFileName() const { return logfile.fileName(); }
109 
setChannelEncoding(const QString &)110         virtual void setChannelEncoding(const QString& /* encoding */) {}
getChannelEncoding()111         virtual QString getChannelEncoding() const { return QString(); }
getChannelEncodingDefaultDesc()112         virtual QString getChannelEncodingDefaultDesc() const { return QString(); }
113         bool isChannelEncodingSupported() const;
114 
115         /** Force updateInfo(info) to be emitted.
116          *  Useful for when this tab has just gained focus
117          */
118         virtual void emitUpdateInfo();
119 
120         /** child classes have to override this and return true if they want the
121          *  "insert character" item on the menu to be enabled.
122          */
isInsertSupported()123         virtual bool isInsertSupported() const { return m_inputBar != nullptr; }
124 
125         /** child classes have to override this and return true if they want the
126          *  "irc color" item on the menu to be enabled.
127          */
areIRCColorsSupported()128         virtual bool areIRCColorsSupported() const {return false; }
129 
currentTabNotification()130         Konversation::TabNotifyType currentTabNotification() const { return m_currentTabNotify; }
131         QColor highlightColor();
132 
133         void msgHelper(const QString& recipient, const QString& message);
134 
setMargin(int margin)135         void setMargin(int margin) { layout()->setContentsMargins(margin, margin, margin, margin); }
setSpacing(int spacing)136         void setSpacing(int spacing) { layout()->setSpacing(spacing); }
137         void activateView();
138 
139     Q_SIGNALS:
140         void nameChanged(ChatWindow* view, const QString& newName);
141         //void online(ChatWindow* myself, bool state);
142         /** Emit this signal when you want to change the status bar text for this tab.
143          *  It is ignored if this tab isn't focused.
144          */
145         void updateInfo(const QString &info);
146         void updateTabNotification(ChatWindow* chatWin, const Konversation::TabNotifyType& type);
147 
148         void setStatusBarTempText(const QString&);
149         void clearStatusBarTempText();
150 
151         void closing(ChatWindow* myself);
152         void showView(ChatWindow* myself);
153         void windowActivationRequested();
154 
155     public Q_SLOTS:
156         virtual void updateAppearance();
157 
158         void logText(const QString& text);
159 
160         /**
161          * This is called when a chat window gains focus.
162          * It enables and disables the appropriate menu items,
163          * then calls childAdjustFocus.
164          * You can call this manually to focus this tab.
165          */
166         void adjustFocus();
167 
168         virtual void appendInputText(const QString& text, bool fromCursor);
169         virtual void indicateAway(bool away);
170 
171 
setNotificationsEnabled(bool enable)172         virtual void setNotificationsEnabled(bool enable) { m_notificationsEnabled = enable; }
173         void activateTabNotification(Konversation::TabNotifyType type);
174         void resetTabNotification();
175 
176     protected Q_SLOTS:
177         ///Used to disable functions when not connected
178         virtual void serverOnline(bool online);
179 
180     protected:
181         void childEvent(QChildEvent* event) override;
182 
183         /** Some children may handle the name themselves, and not want this public.
184          *  Increase the visibility in the subclass if you want outsiders to call this.
185          *  The name is the string that is shown in the tab.
186          *  @param newName The name to show in the tab
187          */
188         virtual void setName(const QString& newName);
189 
190         /** Called from adjustFocus */
191         virtual void childAdjustFocus() = 0;
192 
193         void setLogfileName(const QString& name);
194         void setChannelEncodingSupported(bool enabled);
195         void cdIntoLogPath();
196 
197         int spacing();
198         int margin();
199 
200     protected:
201         bool firstLog;
202         QString name;
203         QString logName;
204 
205         QFont font;
206 
207         IRCView* textView;
208         /** A pointer to the server this chatwindow is part of.
209          *  Not always non-null - e.g. for konsolepanel
210          */
211 
212         IRCInput* m_inputBar;
213 
214         Server* m_server;
215         QFile logfile;
216         WindowType type;
217 
218         bool m_isTopLevelView;
219 
220         bool m_notificationsEnabled;
221 
222         bool m_channelEncodingSupported;
223 
224         Konversation::TabNotifyType m_currentTabNotify;
225 
226         bool m_recreationScheduled;
227 
228         Q_DISABLE_COPY(ChatWindow)
229 };
230 
231 #endif
232