1 /*
2     Copyright © 2015-2019 by The qTox Project Contributors
3 
4     This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6     qTox is libre software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     qTox 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 qTox.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #ifndef PROFILE_H
22 #define PROFILE_H
23 
24 #include "src/core/core.h"
25 #include "src/core/toxencrypt.h"
26 #include "src/core/toxid.h"
27 
28 #include "src/persistence/history.h"
29 
30 #include <QByteArray>
31 #include <QObject>
32 #include <QPixmap>
33 #include <QString>
34 #include <QVector>
35 #include <memory>
36 
37 class Settings;
38 class QCommandLineParser;
39 
40 class Profile : public QObject
41 {
42     Q_OBJECT
43 
44 public:
45     static Profile* loadProfile(QString name, const QCommandLineParser* parser, const QString& password = QString());
46     static Profile* createProfile(QString name, const QCommandLineParser* parser, QString password);
47     ~Profile();
48 
49     Core* getCore();
50     QString getName() const;
51 
52     void startCore();
53     bool isEncrypted() const;
54     QString setPassword(const QString& newPassword);
55     const ToxEncrypt* getPasskey() const;
56 
57     QPixmap loadAvatar();
58     QPixmap loadAvatar(const ToxPk& owner);
59     QByteArray loadAvatarData(const ToxPk& owner);
60     void setAvatar(QByteArray pic);
61     void setFriendAvatar(const ToxPk& owner, QByteArray pic);
62     QByteArray getAvatarHash(const ToxPk& owner);
63     void removeSelfAvatar();
64     void removeFriendAvatar(const ToxPk& owner);
65     bool isHistoryEnabled();
66     History* getHistory();
67 
68     QStringList remove();
69 
70     bool rename(QString newName);
71 
72     static const QStringList getAllProfileNames();
73 
74     static bool exists(QString name);
75     static bool isEncrypted(QString name);
76     static QString getDbPath(const QString& profileName);
77 
78 signals:
79     void selfAvatarChanged(const QPixmap& pixmap);
80     // emit on any change, including default avatar. Used by those that don't care about active on default avatar.
81     void friendAvatarChanged(const ToxPk& friendPk, const QPixmap& pixmap);
82     // emit on a set of avatar, including identicon, used by those two care about active for default, so can't use friendAvatarChanged
83     void friendAvatarSet(const ToxPk& friendPk, const QPixmap& pixmap);
84     // emit on set to default, used by those that modify on active
85     void friendAvatarRemoved(const ToxPk& friendPk);
86     // TODO(sudden6): this doesn't seem to be the right place for Core errors
87     void failedToStart();
88     void badProxy();
89     void coreChanged(Core& core);
90 
91 public slots:
92     void onRequestSent(const ToxPk& friendPk, const QString& message);
93 
94 private slots:
95     void loadDatabase(QString password);
96     void saveAvatar(const ToxPk& owner, const QByteArray& avatar);
97     void removeAvatar(const ToxPk& owner);
98     void onSaveToxSave();
99     // TODO(sudden6): use ToxPk instead of friendId
100     void onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QByteArray& avatarHash);
101 
102 private:
103     Profile(QString name, const QString& password, bool newProfile, const QByteArray& toxsave,
104             std::unique_ptr<ToxEncrypt> passKey);
105     static QStringList getFilesByExt(QString extension);
106     QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
107     bool saveToxSave(QByteArray data);
108     void initCore(const QByteArray& toxsave, ICoreSettings& s, bool isNewProfile);
109 
110 private:
111     std::unique_ptr<Core> core = nullptr;
112     QString name;
113     std::unique_ptr<ToxEncrypt> passkey = nullptr;
114     std::shared_ptr<RawDatabase> database;
115     std::shared_ptr<History> history;
116     bool isRemoved;
117     bool encrypted = false;
118     static QStringList profiles;
119 };
120 
121 #endif // PROFILE_H
122