1 /*
2  * Copyright 2014 Canonical Ltd.
3  * Authors:
4  *      Roberto Mier
5  *      Tiago Herrmann
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 3.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef SETTINGS_H
22 #define SETTINGS_H
23 
24 #define PROG_NAME "libqtelegram"
25 #define CONFIG_DIRECTORY "." PROG_NAME
26 #define CONFIG_FILE "config"
27 #define AUTH_KEY_FILE "auth"
28 #define STATE_FILE "state"
29 #define SECRET_CHAT_FILE "secret"
30 #define DEFAULT_CONFIG_CONTENTS     \
31   "# This is an empty config file\n" \
32   "# Feel free to put something here\n"
33 #define ST_DC_NUM "dcNum"
34 #define ST_AUTH_FILE "authFile"
35 #define ST_SECRET "secret"
36 #define ST_DOWNLOADS "downloads"
37 #define ST_STATE_FILE "stateFile"
38 #define ST_TEST_MODE "testMode"
39 #define ST_MANAGED_DOWNLOADS "managedDownloads"
40 #define ST_LANG_CODE "lang"
41 #define ST_RESEND_QUERIES "resendQueries"
42 
43 #define ST_WORKING_DC_NUM "workingDcNum"
44 #define ST_DC_STATE "state"
45 #define ST_OUR_ID "ourId"
46 #define ST_DCS_ARRAY "dcs"
47 #define ST_HOST "host"
48 #define ST_PORT "port"
49 #define ST_AUTH_KEY_ID "authKeyId"
50 #define ST_AUTH_KEY "authKey"
51 #define ST_SERVER_SALT "serverSalt"
52 #define ST_EXPIRES "expires"
53 #define ST_PRODUCTION "production"
54 #define ST_TEST "test"
55 
56 #define ST_VERSION "version"
57 #define ST_G "g"
58 #define ST_P "p"
59 #define ST_SECRET_CHATS_ARRAY "secretChats"
60 #define ST_CHAT_ID "chatId"
61 #define ST_ACCESS_HASH "accessHash"
62 #define ST_ADMIN_ID "adminId"
63 #define ST_PARTICIPANT_ID "participantId"
64 #define ST_DATE "date"
65 #define ST_SHARED_KEY "sharedKey"
66 #define ST_LAYER "layer"
67 #define ST_IN_SEQ_NO "inSeqNo"
68 #define ST_OUT_SEQ_NO "outSeqNo"
69 #define ST_STATE "state"
70 #define ST_MY_KEY "myKey"
71 
72 #define DEFAULT_CONFIG_PATH "~/.libqtelegram"
73 #define DEFAULT_PUBLIC_KEY_FILE "qtelegram.pub"
74 
75 #include <QObject>
76 #include <QList>
77 #include <QLoggingCategory>
78 #include <openssl/rsa.h>
79 #include "secret/secretchat.h"
80 #include "libqtelegram_global.h"
81 
Q_DECLARE_LOGGING_CATEGORY(TG_CORE_SETTINGS)82 Q_DECLARE_LOGGING_CATEGORY(TG_CORE_SETTINGS)
83 
84 class DC;
85 
86 class LIBQTELEGRAMSHARED_EXPORT Settings : public QObject
87 {
88     Q_OBJECT
89 public:
90     Settings();
91     ~Settings();
92     Settings(const Settings &); // hide copy constructor
93     Settings& operator=(const Settings &); // hide asignment
94 
95     void setDefaultHostAddress(const QString &host);
96     void setDefaultHostPort(qint16 port);
97     void setDefaultHostDcId(qint16 dcId);
98     void setAppId(qint32 appId);
99     void setAppHash(const QString &appHash);
100 
101     QString defaultHostAddress();
102     qint16 defaultHostPort();
103     qint16 defaultHostDcId();
104     qint32 appId();
105     QString appHash();
106 
107     bool loadSettings(const QString &phoneNumber, const QString &configPath = DEFAULT_CONFIG_PATH, const QString &publicKeyFile = DEFAULT_PUBLIC_KEY_FILE);
108     void writeAuthFile();
109     void writeSecretFile();
110     bool workingDcConfigAvailabe() const {return  m_workingDcConfigAvailabe;}
111 
112     qint64 pkFingerprint() const { return m_pkFingerprint; }
113     RSA* pubKey() { return m_pubKey; }
114     qint32 workingDcNum() const { return m_workingDcNum; }
115     qint32 ourId() const { return m_ourId; }
116     QList<DC *> &dcsList() { return m_dcsList; }
117     QString phoneNumber() const { return m_phoneNumber; }
118     bool testMode() const { return m_testMode; }
119     bool managedDownloads() const { return m_managedDownloads; }
120     QString langCode() const { return m_langCode; }
121     bool resendQueries() const { return mResendQueries; }
122     qint32 version() const { return mVersion; }
123     qint32 g() const { return mG; }
124     QByteArray p() const { return mP; }
125     QList<SecretChat *> &secretChats() { return mSecretChats; }
126     void setWorkingDcNum(qint32 workingDcNum) { m_workingDcNum = workingDcNum; m_workingDcConfigAvailabe = true; }
127     void setOurId(qint32 ourId) { m_ourId = ourId; }
128     void setDcsList(const QList<DC *> &dcsList) { m_dcsList = dcsList; }
129     void setVersion(qint32 version) { mVersion = version; }
130     void setG(qint32 g) { mG = g; }
131     void setP(const QByteArray &p) { mP = p; }
132     void setSecretChats(const QList<SecretChat *> &secretChats) { mSecretChats = secretChats; }
133 
134     bool removeAuthFile();
135     void writeCrashFile();
136 
137     typedef bool (*ReadFunc)(const QString &configPath, const QString &phone, QVariantMap &map);
138     typedef bool (*WriteFunc)(const QString &configPath, const QString &phone, const QVariantMap &map);
139 
140     static void setAuthConfigMethods(ReadFunc readFunc, WriteFunc writeFunc);
141     static void clearAuth(const QString &configPath, const QString &phone);
142 
143 private:
144     void readAuthFile();
145     void readSecretFile();
146 
147     QString m_defaultHostAddress;
148     qint16 m_defaultHostPort;
149     qint16 m_defaultHostDcId;
150     qint32 m_appId;
151     QString m_appHash;
152 
153     bool m_workingDcConfigAvailabe;
154     qint64 m_pkFingerprint;
155     RSA *m_pubKey;
156     QString m_authFilename;
157     QString m_secretChatFilename;
158     QString m_stateFilename;
159     qint32 m_workingDcNum;
160     qint32 m_ourId;
161     QList<DC *> m_dcsList;
162     QString m_phoneNumber;
163     QString m_baseConfigDirectory;
164     // if true, all operations are performed against telegram test servers
165     bool m_testMode;
166     // if true, downloading a big file will be stored in byte array until completion and only one signal is emited when download finish
167     // if false, every downloaded part is notified as a signal with the partial data. Default is false
168     bool m_managedDownloads;
169     // default is 'en'
170     QString m_langCode;
171     // default is false. If true, queries not acked after 30 seconds will be re-sent
172     bool mResendQueries;
173 
174     qint32 mVersion;
175     qint32 mG;
176     QByteArray mP;
177     QList<SecretChat *> mSecretChats;
178 
179 };
180 
181 #endif // SETTINGS_H
182