otr: removed TLV parsing code (i still has to fix SMP queries in callback)
[dyskinesia.git] / src / chatform.h
blob970870a39b0b4731b10bbc72a2aca98b6cfdf4bf
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://sam.zoy.org/wtfpl/COPYING for more details.
9 */
10 #ifndef CHATFORM_H
11 #define CHATFORM_H
13 #ifdef USE_OTR
14 extern "C" {
15 # include <libotr/proto.h>
16 # include <libotr/context.h>
17 # include <libotr/userstate.h>
18 # include <libotr/privkey.h>
19 # include <libotr/tlv.h>
20 # include <libotr/message.h>
22 class GenKeyThread;
23 #endif
25 #include <QContextMenuEvent>
26 #include <QDesktopServices>
27 #include <QHash>
28 #include <QLabel>
29 #include <QList>
30 #include <QMap>
31 #include <QNetworkAccessManager>
32 #include <QNetworkReply>
33 #include <QNetworkRequest>
34 #include <QSet>
35 #include <QShortcut>
36 #include <QString>
37 #include <QSystemTrayIcon>
38 #include <QWebFrame>
39 #include <QWidget>
40 #include <QWidgetAction>
42 #include "psycproto.h"
43 #include "k8popups.h"
44 #include "k8sdb.h"
46 #include "floatwin.h"
47 #include "keycmb.h"
49 #include "floatman.h"
52 #ifdef WIN32
53 # define MAIN_TRAY_ICON ":/icons/tray/main.png"
54 # define MSG_TRAY_ICON ":/icons/tray/message.png"
55 #else
56 # define MAIN_TRAY_ICON ":/icons/tray/solid/main.png"
57 # define MSG_TRAY_ICON ":/icons/tray/solid/message.png"
58 #endif
61 class QSettings;
62 class PsycPacket;
63 class PsycContact;
64 class PsycPlace;
65 class ConsoleForm;
66 class ChatForm;
67 class AccWindow;
69 class EngineCommands;
70 class EngineBindings;
71 class FloatersManager;
72 class ChatJSAPI;
75 ///////////////////////////////////////////////////////////////////////////////
76 class EnterFilter : public QObject {
77 Q_OBJECT
79 public:
80 EnterFilter (QObject *parent=0);
81 ~EnterFilter ();
84 protected:
85 bool eventFilter (QObject *obj, QEvent *event);
88 private:
89 ChatForm *mForm;
90 bool mCtrlDown;
94 ///////////////////////////////////////////////////////////////////////////////
95 class Option {
96 public:
97 Option (const QString &def, const QString &aHelp);
99 inline bool isValid () const { return mValid; }
101 enum Scope {
102 Both,
103 OnlyGlobal,
104 OnlyLocal
107 public:
108 QString name;
109 QString dbName;
110 K8SDB::Type type;
111 Scope scope;
112 QString defVal;
113 QString help;
115 private:
116 bool parseDef (const QString &def);
118 private:
119 bool mValid;
123 ///////////////////////////////////////////////////////////////////////////////
124 class NetworkAccessManager : public QNetworkAccessManager {
125 Q_OBJECT
127 public:
128 NetworkAccessManager (QObject *parent = 0);
130 protected:
131 QNetworkReply *createRequest (Operation op, const QNetworkRequest &req, QIODevice *outgoingData = 0);
135 ///////////////////////////////////////////////////////////////////////////////
136 #include "ui_chatform.h"
137 class ChatForm : public QWidget, public Ui_ChatForm {
138 Q_OBJECT
139 friend class EnterFilter;
140 friend class ChatJSAPI;
141 friend class EngineCommands;
142 friend class EngineBindings;
144 public:
145 ChatForm (QWidget *parent=0);
146 ~ChatForm ();
148 // doesn't clear uni/pass
149 void clear ();
151 const QString &accName () const { return mAccName; }
153 PsycProto *proto () const { return mProto; }
155 inline void setPassword (const QString &aPassword) { mPassword = aPassword; }
156 inline QString password () { return mPassword; }
158 void scrollToBottom ();
160 enum ACSaveMode { SaveContact, DontSaveContact, TempContact };
162 PsycContact *findContact (const QString &uni);
163 PsycContact *findContact (const PsycEntity &cont);
164 PsycContact *addContact (const PsycEntity &user, const QString &group=QString(""), bool *isNew=0, ACSaveMode mode=SaveContact);
165 void redrawContact (PsycContact *cc);
166 void deleteContact (PsycContact *cc);
168 QIcon statusIcon (PsycProto::Status st);
169 void setStatus (PsycProto::Status st, bool doSend=true);
170 void setMood (PsycProto::Mood md);
172 void startChatWith (const QString &aUNI, bool dontRearrange=false);
174 void clearChat ();
175 void clearCList ();
177 // settings engine
178 bool removeOpt (const QString &name, const QString &aUNI=QString(""));
179 bool hasKeyOpt (const QString &name, const QString &aUNI=QString("")) const;
180 K8SDB::Type typeOpt (const QString &name, const QString &aUNI=QString(""));
182 bool setOpt (const QString &name, bool v, const QString &aUNI=QString(""));
183 bool setOpt (const QString &name, qint32 v, const QString &aUNI=QString(""));
184 bool setOpt (const QString &name, const QString &v, const QString &aUNI=QString(""));
185 inline bool setOpt (const QString &name, const char *v, const QString &aUNI=QString("")) { return setOpt(name, QString(v), aUNI); }
186 bool setOpt (const QString &name, const QKeySequence &v, const QString &aUNI=QString(""));
187 bool setOpt (const QString &name, K8SDB::Type type, const QString &v, const QString &aUNI=QString(""));
189 bool toBoolOpt (const QString &name, bool def=false, const QString &aUNI=QString(""));
190 qint32 toIntOpt (const QString &name, qint32 def=0, const QString &aUNI=QString(""));
191 QString toStringOpt (const QString &name, const QString &def=QString(), const QString &aUNI=QString(""));
192 QByteArray toBAOpt (const QString &name, const QByteArray &def=QByteArray(), const QString &aUNI=QString(""));
193 QKeySequence toKeySequenceOpt (const QString &name, const QKeySequence &def=QKeySequence(), const QString &aUNI=QString(""));
195 QString asStringOpt (const QString &name, const QString &aUNI=QString(""));
197 bool removeOptCU (const QString &name);
198 bool hasKeyOptCU (const QString &name) const;
199 K8SDB::Type typeOptCU (const QString &name);
201 bool setOptCU (const QString &name, bool v);
202 bool setOptCU (const QString &name, qint32 v);
203 bool setOptCU (const QString &name, const QString &v);
204 inline bool setOptCU (const QString &name, const char *v) { return setOptCU(name, QString(v)); }
205 bool setOptCU (const QString &name, const QKeySequence &v);
206 bool setOptCU (const QString &name, K8SDB::Type type, const QString &v);
208 bool toBoolOptCU (const QString &name, bool def=false);
209 qint32 toIntOptCU (const QString &name, qint32 def=0);
210 QString toStringOptCU (const QString &name, const QString &def=QString());
211 QByteArray toBAOptCU (const QString &name, const QByteArray &def=QByteArray());
212 QKeySequence toKeySequenceOptCU (const QString &name, const QKeySequence &def=QKeySequence());
214 QString asStringOptCU (const QString &name);
216 #ifdef USE_OTR
217 char *getOTRKeyFile (void);
218 char *getOTRFingerFile (void);
219 void otrSendMessage (const QString &auni, const QString &txt, const QString &act);
220 QString otrGotMessage (const QString &auni, const QString &txt); // can return empty string for "internal" messages
221 void genOTRKey (const char *accName, const char *protoName);
222 #endif
223 void otrConnect (const QString &uni);
224 void otrDisconnect (const QString &uni);
225 void otrSetTrust (const QString &aUNI, bool tflag);
226 void otrForget (const QString &aUNI);
227 void otrInitiateSMP (const QString &aUNI, const QString &secret, const QString &question);
228 const QString otrGetFinger (const QString &aUNI);
230 void clearTabHistory (void);
232 void recreatePopMan ();
234 protected:
235 void keyPressEvent (QKeyEvent *event);
236 void showEvent (QShowEvent *event);
237 void closeEvent (QCloseEvent *event);
239 void doSendMessage ();
241 private slots:
242 void onUpdateAccountInfo (AccWindow *au, const QString &text);
243 void onAccountCreated (AccWindow *au, const QString &text);
244 void onAccountDeleted (AccWindow *au, const QString &text);
246 void javaScriptWindowObjectCleared ();
247 void onWebContextMenuNone (const QWebHitTestResult &, const QPoint &) {}
249 void onPopupClick (const QString &type, const QString &id, Qt::MouseButton button);
251 void windowDestroyed ();
252 void consoleDestroyed ();
253 //void splitterMoved (int pos, int index);
255 void on_splitter0_splitterMoved (int pos, int index);
256 void on_splitter1_splitterMoved (int pos, int index);
258 void on_edChat_textChanged (void);
260 void on_action_quit_triggered (bool);
261 void on_action_accedit_triggered (bool);
263 void on_action_showconsole_triggered (bool);
264 void on_action_sendpacket_triggered (bool);
266 void on_action_join_triggered (bool);
268 void onEnterCustomPlace (const PsycEntity &place);
270 void onUserPktSend ();
272 void on_action_stoffline_triggered (bool);
273 void on_action_stvacation_triggered (bool);
274 void on_action_staway_triggered (bool);
275 void on_action_stdnd_triggered (bool);
276 void on_action_stnear_triggered (bool);
277 void on_action_stbusy_triggered (bool);
278 void on_action_sthere_triggered (bool);
279 void on_action_stffc_triggered (bool);
280 void on_action_strtime_triggered (bool);
282 void on_action_moodunspecified_triggered (bool);
283 void on_action_mooddead_triggered (bool);
284 void on_action_moodangry_triggered (bool);
285 void on_action_moodsad_triggered (bool);
286 void on_action_moodmoody_triggered (bool);
287 void on_action_moodokay_triggered (bool);
288 void on_action_moodgood_triggered (bool);
289 void on_action_moodhappy_triggered (bool);
290 void on_action_moodbright_triggered (bool);
291 void on_action_moodnirvana_triggered (bool);
293 void onConnected ();
294 void onDisconnected ();
295 void onNetError (const QString &errStr);
296 void onPacket (const PsycPacket &pkt);
297 void onPacketSent (const PsycPacket &pkt);
299 void onNeedPassword ();
300 void onInvalidPassword ();
301 void onLoginComplete ();
303 void onUserStatusChanged (const PsycEntity &user);
304 void onSelfStatusChanged ();
306 void onAuthRequested (const PsycEntity &user);
307 void onAuthGranted (const PsycEntity &user);
308 void onAuthDenied (const PsycEntity &user);
310 void onAuthClicked (const PsycEntity &entity, const QString &text);
312 void onNotificationType (const PsycEntity &dest);
314 void onPlaceEntered (const PsycEntity &place);
315 void onPlaceLeaved (const PsycEntity &place);
316 void onUserEntersPlace (const PsycEntity &place, const PsycEntity &who);
317 void onUserLeavesPlace (const PsycEntity &place, const PsycEntity &who);
319 void onMessage (const PsycEntity &place, const PsycEntity &user, const QDateTime &time, const QString &text,
320 const QString &action, const QString &forPopup=QString());
321 void onMessageEcho (const PsycEntity &place, const PsycEntity &user, const PsycEntity &target, const QDateTime &time,
322 const QString &text, const QString &action, const QString &tag);
323 void onHistoryMessage (const PsycEntity &place, const PsycEntity &user, const QDateTime &time, const QString &text,
324 const QString &action);
326 void onScratchpadUpdated (const QString &spUrl);
327 void onPlaceWebExam (const PsycEntity &place, const PsycPacket &pkt);
328 void onPlaceNews (const PsycEntity &place, const QString &channel, const QString &title, const QString &url);
330 void onNoticeUpdate (const PsycEntity &place, const PsycPacket &pkt);
332 void onLoadFinishedChat (bool ok);
333 void onLoadFinishedCList (bool ok);
335 void onTrayActivate (QSystemTrayIcon::ActivationReason reason);
336 void onTrayBlink ();
337 //void checkForUpdates ();
339 void onLinkClicked (const QUrl &url);
341 void onOptChanged__tray_visible (const QString &uni);
342 void onOptChanged__tray_blinktime (const QString &uni);
343 void onOptChanged__tray_notify (const QString &uni);
344 void onOptChanged__contactlist_onright (const QString &uni);
345 void onOptChanged__contactlist_showalways (const QString &uni);
347 //void onTestSCutActivated ();
349 void onShortcutOSmile ();
350 void onShortcutCatoSmile ();
351 void onShortcutDesu ();
352 void onShortcutRecode ();
353 void onShortcutQuote ();
354 void onShortcutCopy ();
355 void onShortcutUnsel ();
356 void onShortcutUnread ();
357 void onShortcutCloseChat ();
358 void onShortcutActivateEditor ();
359 void onShortcutCheckBlink ();
360 void onShortcutSetStatus ();
363 void onGShortcutShow ();
364 void onGShortcutHidePopups ();
366 void onFloatyDblClicked (int x, int y, const QString &uni);
368 private:
369 void setAccountShortcuts ();
371 void showPopup (const PsycEntity &place, const PsycEntity &from,
372 const QString &type, const QString &id, const QString &msg, const QString &action, bool update=false);
373 void showPrivatePopup (const PsycEntity &from, const QString &msg, const QString &action);
374 void showPublicPopup (const PsycEntity &place, const PsycEntity &from, const QString &msg, const QString &action);
375 void showStatusPopup (const PsycEntity &from);
376 void showInfoPopup (const QString &msg);
377 void showErrorPopup (const QString &msg);
379 QIcon LinuxizeTrayIcon (const QIcon &ico);
380 void checkBlink ();
382 void optionChangedSignal (const QString &name, const QString &uni);
384 void optPrint (const QString &s); // html
386 void clearOptionList ();
387 void clearBindings ();
388 void loadOptionList ();
389 bool loadBindings (const QString &fname);
390 void setAccDefaultOptions (K8SDB *db);
392 void accountSetDefaults (K8SDB *db);
393 void saveOneContact (PsycContact *cc);
394 void loadContactUnread (PsycContact *cc);
395 void loadContactList ();
396 void loadAutosaved ();
397 void loadAccount ();
398 void setDefAccount (const QString &name);
399 QString defAccount ();
401 void storeAppData ();
402 void requestStoredAppData ();
403 void parseStoredAppData (const PsycPacket &pkt);
405 void loadRecodeMap (void);
406 QString recodeMsg (const QString &msg);
407 void translateMessage ();
409 void packetToJSAPI (const PsycPacket &pkt); // export packet to WebKit JS
410 void runChatJS (const QString &js);
411 void runCListJS (const QString &js);
413 enum UnreadFlag { MessageRead, MessageUnread };
415 bool addMessageToHistory (const PsycEntity &dest, const PsycEntity &src, const QString &text,
416 const QString &action, const QDateTime &time, UnreadFlag isUnread);
418 bool addMessageToChat (const PsycEntity &from, const QString &msg, const QString &action, const QDateTime &date);
420 void setControlsAccActive (bool accAct);
422 void markAsRead (const QString &aUNI);
424 void switchTab (bool done);
425 void switchToUnread ();
427 QStringList findByUniPart (const QString &up) const;
428 PsycContact *findByUniOne (QString up) const;
430 void deactivateAllOTR ();
431 void disconnectAllOTR ();
433 void chatAutoComplete ();
435 bool processKeyCombo (QKeyEvent *keyEvent);
436 void refreshBindings ();
438 private:
439 QString mVerStr;
441 ConsoleForm *mConForm;
442 QSystemTrayIcon *mTrayIcon;
444 QString mAccName;
445 PsycProto *mProto;
446 QString mPassword;
447 QHash<QString, PsycContact *> mContactList;
448 QSet<QString> mWasSentTo;
450 //QTimer *mSaveTimer;
451 PsycProto::Status mMyStatus;
452 PsycProto::Mood mMyMood;
454 bool mChBlock;
456 QHash<QChar, QChar> mRecodeMap;
457 EnterFilter *mEFilter;
458 PsycContact *mChatUser;
459 QString mSelText;
461 K8PopupManager *mPopMan;
462 K8PopupManager::Position mPopPos;
464 bool mCloseWaiting;
465 bool mServerStore;
466 bool mPopupActive;
467 QString mURLStyle;
469 ChatJSAPI *mJSAPI;
470 ChatJSAPI *mCLJSAPI;
472 K8SDB *mSDB;
473 QTimer *mBlinkTimer;
474 bool mBlinkMsg;
475 bool mTrayMsgNoteActive;
476 bool mUnreadInChat;
478 bool mCListOnRight;
479 bool mDoTypo;
481 bool mChatLoaded;
482 bool mCListLoaded;
484 QHash<QString, Option *> mOptionList;
485 QList<PsycContact *> mTabList;
486 bool mTabDown;
487 int mTabSwitchNo;
488 bool mWasAtBottom;
489 //QSet<QString> mIgnores;
491 QString mStartingNickCompletion;
492 QString mLatestNickCompletion;
494 FloatersManager *mFMan;
496 KeyComboList *mCurCombos;
497 KeyComboList *mComboList;
498 KeyComboList *mEditComboList;
499 bool mPrevFocusWasEdit;
501 EngineCommands *mEngCmds;
502 EngineBindings *mEngBinds;
504 public:
505 #ifdef USE_OTR
506 void restartOTRTimer (unsigned int interval);
507 QTimer *mOTRTimer;
508 void onOTRTimer ();
509 OtrlUserState mOTRState;
510 friend class GenKeyThread;
511 #endif
515 #endif