qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
profile.h
Go to the documentation of this file.
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 #pragma once
22 
23 #include "src/core/core.h"
24 #include "src/core/toxencrypt.h"
25 
27 
30 
31 #include <QByteArray>
32 #include <QObject>
33 #include <QPixmap>
34 #include <QString>
35 #include <QVector>
36 #include <memory>
37 
38 class Settings;
39 class QCommandLineParser;
40 class ToxPk;
41 
42 class Profile : public QObject
43 {
44  Q_OBJECT
45 
46 public:
47  static Profile* loadProfile(const QString& name, const QString& password, Settings& settings,
48  const QCommandLineParser* parser);
49  static Profile* createProfile(const QString& name, const QString& password, Settings& settings,
50  const QCommandLineParser* parser);
51  ~Profile();
52 
53  Core& getCore() const;
54  QString getName() const;
55 
56  void startCore();
57  bool isEncrypted() const;
58  QString setPassword(const QString& newPassword);
59  const ToxEncrypt* getPasskey() const;
60 
61  QPixmap loadAvatar();
62  QPixmap loadAvatar(const ToxPk& owner);
63  QByteArray loadAvatarData(const ToxPk& owner);
64  void setAvatar(QByteArray pic);
65  void setFriendAvatar(const ToxPk& owner, QByteArray pic);
66  QByteArray getAvatarHash(const ToxPk& owner);
67  void removeSelfAvatar();
68  void removeFriendAvatar(const ToxPk& owner);
69  bool isHistoryEnabled();
71 
72  QStringList remove();
73 
74  bool rename(QString newName);
75 
76  static const QStringList getAllProfileNames();
77 
78  static bool exists(QString name);
79  static bool isEncrypted(QString name);
80  static QString getDbPath(const QString& profileName);
81 
82 signals:
83  void selfAvatarChanged(const QPixmap& pixmap);
84  // emit on any change, including default avatar. Used by those that don't care about active on default avatar.
85  void friendAvatarChanged(const ToxPk& friendPk, const QPixmap& pixmap);
86  // emit on a set of avatar, including identicon, used by those two care about active for default, so can't use friendAvatarChanged
87  void friendAvatarSet(const ToxPk& friendPk, const QPixmap& pixmap);
88  // emit on set to default, used by those that modify on active
89  void friendAvatarRemoved(const ToxPk& friendPk);
90  // TODO(sudden6): this doesn't seem to be the right place for Core errors
91  void failedToStart();
92  void badProxy();
93  void coreChanged(Core& core);
94 
95 public slots:
96  void onRequestSent(const ToxPk& friendPk, const QString& message);
97 
98 private slots:
99  void loadDatabase(QString password);
100  void saveAvatar(const ToxPk& owner, const QByteArray& avatar);
101  void removeAvatar(const ToxPk& owner);
102  void onSaveToxSave();
103  // TODO(sudden6): use ToxPk instead of friendId
104  void onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QByteArray& avatarHash, uint64_t filesize);
105 
106 private:
107  Profile(const QString& name, std::unique_ptr<ToxEncrypt> passkey, Paths& paths, Settings &settings_);
108  static QStringList getFilesByExt(QString extension);
109  QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
110  bool saveToxSave(QByteArray data);
111  void initCore(const QByteArray& toxsave, Settings &s, bool isNewProfile);
112 
113 private:
114  std::unique_ptr<AvatarBroadcaster> avatarBroadcaster;
115  std::unique_ptr<Core> core;
116  std::unique_ptr<CoreAV> coreAv;
117  QString name;
118  std::unique_ptr<ToxEncrypt> passkey;
119  std::shared_ptr<RawDatabase> database;
120  std::shared_ptr<History> history;
121  bool isRemoved;
122  bool encrypted = false;
123  static QStringList profiles;
124  std::unique_ptr<BootstrapNodeUpdater> bootstrapNodes;
127 };
Profile::core
std::unique_ptr< Core > core
Definition: profile.h:115
Profile::~Profile
~Profile()
Definition: profile.cpp:376
Settings
Definition: settings.h:51
history.h
Profile::database
std::shared_ptr< RawDatabase > database
Definition: profile.h:119
Profile::initCore
void initCore(const QByteArray &toxsave, Settings &s, bool isNewProfile)
Definition: profile.cpp:235
Profile::failedToStart
void failedToStart()
Profile::badProxy
void badProxy()
Profile::passkey
std::unique_ptr< ToxEncrypt > passkey
Definition: profile.h:118
Profile
Handles all qTox internal paths.
Definition: profile.h:42
Profile::rename
bool rename(QString newName)
Tries to rename the profile.
Definition: profile.cpp:896
Profile::profiles
static QStringList profiles
Definition: profile.h:123
Paths
Definition: paths.h:28
Profile::getPasskey
const ToxEncrypt * getPasskey() const
Definition: profile.cpp:922
Profile::removeFriendAvatar
void removeFriendAvatar(const ToxPk &owner)
Removes friend avatar.
Definition: profile.cpp:766
Profile::onRequestSent
void onRequestSent(const ToxPk &friendPk, const QString &message)
Adds history message about friendship request attempt if history is enabled.
Definition: profile.cpp:704
Profile::saveToxSave
bool saveToxSave(QByteArray data)
Write the .tox save, encrypted if needed.
Definition: profile.cpp:487
Profile::loadAvatar
QPixmap loadAvatar()
Get our avatar from cache.
Definition: profile.cpp:556
Profile::setPassword
QString setPassword(const QString &newPassword)
Changes the encryption password and re-saves everything with it.
Definition: profile.cpp:934
Profile::avatarPath
QString avatarPath(const ToxPk &owner, bool forceUnencrypted=false)
Gets the path of the avatar file cached by this profile and corresponding to this owner ID.
Definition: profile.cpp:531
Profile::getName
QString getName() const
Definition: profile.cpp:435
Profile::bootstrapNodes
std::unique_ptr< BootstrapNodeUpdater > bootstrapNodes
Definition: profile.h:124
Profile::avatarBroadcaster
std::unique_ptr< AvatarBroadcaster > avatarBroadcaster
Definition: profile.h:114
Profile::getCore
Core & getCore() const
Definition: profile.cpp:428
Profile::saveAvatar
void saveAvatar(const ToxPk &owner, const QByteArray &avatar)
Save an avatar to cache.
Definition: profile.cpp:722
Profile::isRemoved
bool isRemoved
True if the profile has been removed by remove().
Definition: profile.h:121
Profile::remove
QStringList remove()
Removes the profile permanently. Updates the profiles vector.
Definition: profile.cpp:847
Profile::setFriendAvatar
void setFriendAvatar(const ToxPk &owner, QByteArray pic)
Sets a friends avatar.
Definition: profile.cpp:679
Profile::onAvatarOfferReceived
void onAvatarOfferReceived(uint32_t friendId, uint32_t fileId, const QByteArray &avatarHash, uint64_t filesize)
Definition: profile.cpp:474
bootstrapnodeupdater.h
Profile::removeSelfAvatar
void removeSelfAvatar()
Removes our own avatar.
Definition: profile.cpp:758
Profile::exists
static bool exists(QString name)
Definition: profile.cpp:804
avatarbroadcaster.h
Profile::encrypted
bool encrypted
Definition: profile.h:122
HistMessageContentType::message
@ message
Profile::loadDatabase
void loadDatabase(QString password)
Definition: profile.cpp:616
Profile::friendAvatarRemoved
void friendAvatarRemoved(const ToxPk &friendPk)
ToxPk
This class represents a Tox Public Key, which is a part of Tox ID.
Definition: toxpk.h:26
Profile::getDbPath
static QString getDbPath(const QString &profileName)
Retrieves the path to the database file for a given profile.
Definition: profile.cpp:985
Profile::removeAvatar
void removeAvatar(const ToxPk &owner)
Removes a cached avatar.
Definition: profile.cpp:794
Profile::friendAvatarChanged
void friendAvatarChanged(const ToxPk &friendPk, const QPixmap &pixmap)
Profile::loadAvatarData
QByteArray loadAvatarData(const ToxPk &owner)
Get a contact's avatar from cache.
Definition: profile.cpp:590
ToxEncrypt
Encapsulates the toxencrypsave API. Since key derivation is work intensive and to avoid storing plain...
Definition: toxencrypt.h:29
Profile::getAvatarHash
QByteArray getAvatarHash(const ToxPk &owner)
Get the tox hash of a cached avatar.
Definition: profile.cpp:747
Profile::name
QString name
Definition: profile.h:117
Profile::getHistory
History * getHistory()
Get chat history.
Definition: profile.cpp:785
History
Interacts with the profile database to save the chat history.
Definition: history.h:135
Profile::coreChanged
void coreChanged(Core &core)
Profile::coreAv
std::unique_ptr< CoreAV > coreAv
Definition: profile.h:116
Profile::loadProfile
static Profile * loadProfile(const QString &name, const QString &password, Settings &settings, const QCommandLineParser *parser)
Locks and loads an existing profile and creates the associate Core* instance.
Definition: profile.cpp:313
Profile::history
std::shared_ptr< History > history
Definition: profile.h:120
Profile::settings
Settings & settings
Definition: profile.h:126
toxencrypt.h
core.h
Profile::onSaveToxSave
void onSaveToxSave()
Saves the profile's .tox save, encrypted if needed.
Definition: profile.cpp:466
Profile::paths
Paths & paths
Definition: profile.h:125
Profile::getAllProfileNames
static const QStringList getAllProfileNames()
Scan for profile, automatically importing them if needed.
Definition: profile.cpp:414
Profile::getFilesByExt
static QStringList getFilesByExt(QString extension)
Lists all the files in the config dir with a given extension.
Definition: profile.cpp:395
Profile::createProfile
static Profile * createProfile(const QString &name, const QString &password, Settings &settings, const QCommandLineParser *parser)
Creates a new profile and the associated Core* instance.
Definition: profile.cpp:355
Profile::setAvatar
void setAvatar(QByteArray pic)
Sets our own avatar.
Definition: profile.cpp:648
Profile::isEncrypted
bool isEncrypted() const
Checks, if profile has a password.
Definition: profile.cpp:814
Profile::selfAvatarChanged
void selfAvatarChanged(const QPixmap &pixmap)
Profile::friendAvatarSet
void friendAvatarSet(const ToxPk &friendPk, const QPixmap &pixmap)
Profile::Profile
Profile(const QString &name, std::unique_ptr< ToxEncrypt > passkey, Paths &paths, Settings &settings_)
Definition: profile.cpp:296
Core
Definition: core.h:59
Profile::isHistoryEnabled
bool isHistoryEnabled()
Checks that the history is enabled in the settings, and loaded successfully for this profile.
Definition: profile.cpp:776
Profile::startCore
void startCore()
Starts the Core thread.
Definition: profile.cpp:443