diff --git a/core.cpp b/core.cpp index 7c97d3387..27e0a0b12 100644 --- a/core.cpp +++ b/core.cpp @@ -633,8 +633,8 @@ void Core::onAvatarInfoCallback(Tox*, int32_t friendnumber, uint8_t format, { qDebug() << "Core: Got null avatar info from" << core->getFriendUsername(friendnumber); emit core->friendAvatarRemoved(friendnumber); - QFile::remove(QDir(Settings::getInstance().getSettingsDirPath()).filePath("avatars/"+core->getFriendAddress(friendnumber).left(64)+".png")); - QFile::remove(QDir(Settings::getInstance().getSettingsDirPath()).filePath("avatars/"+core->getFriendAddress(friendnumber).left(64)+".hash")); + QFile::remove(QDir(Settings::getSettingsDirPath()).filePath("avatars/"+core->getFriendAddress(friendnumber).left(64)+".png")); + QFile::remove(QDir(Settings::getSettingsDirPath()).filePath("avatars/"+core->getFriendAddress(friendnumber).left(64)+".hash")); } else { @@ -1183,13 +1183,14 @@ void Core::switchConfiguration(QString profile) toxTimer->stop(); + Widget::getInstance()->clearContactsList(); // we need this to block, so no signals for us + if (tox) { toxav_kill(toxav); toxav = nullptr; tox_kill(tox); tox = nullptr; } - emit clearFriends(); make_tox(); @@ -1210,6 +1211,7 @@ void Core::switchConfiguration(QString profile) void Core::loadFriends() { const uint32_t friendCount = tox_count_friendlist(tox); + qDebug() << "Core: loading" << friendCount << "friends. profile:" << Settings::getInstance().getCurrentProfile(); if (friendCount > 0) { // assuming there are not that many friends to fill up the whole stack int32_t *ids = new int32_t[friendCount]; @@ -1218,7 +1220,7 @@ void Core::loadFriends() for (int32_t i = 0; i < static_cast(friendCount); ++i) { if (tox_get_client_id(tox, ids[i], clientId) == 0) { emit friendAdded(ids[i], CUserId::toString(clientId)); - + qDebug() << "Core: just added friend" << CUserId::toString(clientId); const int nameSize = tox_get_name_size(tox, ids[i]); if (nameSize > 0) { uint8_t *name = new uint8_t[nameSize]; diff --git a/core.h b/core.h index 7062072d3..f57a97342 100644 --- a/core.h +++ b/core.h @@ -40,6 +40,7 @@ public: static const QString TOX_EXT; static const QString CONFIG_FILE_NAME; + static QString sanitize(QString name); int getGroupNumberPeers(int groupId) const; QString getGroupPeerName(int groupId, int peerId) const; @@ -109,7 +110,6 @@ signals: void friendMessageReceived(int friendId, const QString& message, bool isAction); void friendAdded(int friendId, const QString& userId); - void clearFriends(); void friendStatusChanged(int friendId, Status status); void friendStatusMessageChanged(int friendId, const QString& message); @@ -218,7 +218,6 @@ private: bool checkConnection(); bool loadConfiguration(QString path); // Returns false for a critical error, true otherwise - static QString sanitize(QString name); void make_tox(); void loadFriends(); diff --git a/widget/form/settings/identityform.cpp b/widget/form/settings/identityform.cpp index 5aff0e513..a49a43b08 100644 --- a/widget/form/settings/identityform.cpp +++ b/widget/form/settings/identityform.cpp @@ -18,12 +18,17 @@ #include "ui_identitysettings.h" #include "identityform.h" #include "widget/form/settingswidget.h" +#include "misc/settings.h" #include "widget/croppinglabel.h" +#include "widget/widget.h" #include "core.h" #include #include #include #include +#include +#include +#include IdentityForm::IdentityForm() : GenericForm(tr("Your identity"), QPixmap(":/img/settings/identity.png")) @@ -50,6 +55,11 @@ IdentityForm::IdentityForm() : connect(toxId, SIGNAL(clicked()), this, SLOT(copyIdClicked())); connect(bodyUI->userName, SIGNAL(editingFinished()), this, SLOT(onUserNameEdited())); connect(bodyUI->statusMessage, SIGNAL(editingFinished()), this, SLOT(onStatusMessageEdited())); + connect(bodyUI->loadButton, &QPushButton::clicked, this, &IdentityForm::onLoadClicked); + connect(bodyUI->renameButton, &QPushButton::clicked, this, &IdentityForm::onRenameClicked); + connect(bodyUI->exportButton, &QPushButton::clicked, this, &IdentityForm::onExportClicked); + connect(bodyUI->deleteButton, &QPushButton::clicked, this, &IdentityForm::onDeleteClicked); + connect(bodyUI->importButton, &QPushButton::clicked, this, &IdentityForm::onImportClicked); } IdentityForm::~IdentityForm() @@ -77,6 +87,12 @@ void IdentityForm::onStatusMessageEdited() void IdentityForm::updateContent() { toxId->setText(Core::getInstance()->getSelfId().toString()); + bodyUI->profiles->clear(); + for (QString profile : Widget::searchProfiles()) + bodyUI->profiles->addItem(profile); + QString current = Settings::getInstance().getCurrentProfile(); + if (current != "") + bodyUI->profiles->setCurrentText(current); } void IdentityForm::setUserName(const QString &name) @@ -88,3 +104,61 @@ void IdentityForm::setStatusMessage(const QString &msg) { bodyUI->statusMessage->setText(msg); } + +void IdentityForm::onLoadClicked() +{ + Core::getInstance()->switchConfiguration(bodyUI->profiles->currentText()); +} + +void IdentityForm::onRenameClicked() +{ + QString cur = bodyUI->profiles->currentText(); + QString title = tr("Rename \"%1\"", "renaming a profile").arg(cur); + QString name = QInputDialog::getText(this, title, title+":"); + if (name != "") + { + name = Core::sanitize(name); + QDir dir(Settings::getSettingsDirPath()); + QFile::copy(dir.filePath(cur+Core::TOX_EXT), dir.filePath(name+Core::TOX_EXT)); + bodyUI->profiles->setItemText(bodyUI->profiles->currentIndex(), name); + } +} + +void IdentityForm::onExportClicked() +{ + QString current = bodyUI->profiles->currentText() + Core::TOX_EXT; + QString path = QFileDialog::getSaveFileName(this, tr("Export profile", "save dialog title"), + QDir::home().filePath(current), + tr("Tox save file (*.tox)", "save dialog filter")); + QFile::copy(QDir(Settings::getSettingsDirPath()).filePath(current), path); +} + +void IdentityForm::onDeleteClicked() +{ + if (Settings::getInstance().getCurrentProfile() == bodyUI->profiles->currentText()) + { + QMessageBox::warning(this, tr("Profile currently loaded","current profile deletion warning title"), tr("This profile is currently in use. Please load a different profile before deleting this one.","current profile deletion warning text")); + } + else + { + QMessageBox::StandardButton resp = QMessageBox::question(this, + tr("Deletion imminent!","deletion confirmation title"), tr("Are you sure you want to delete this profile?","deletion confirmation text"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No); + if (resp == QMessageBox::Yes) + { + QFile::remove(QDir(Settings::getSettingsDirPath()).filePath(bodyUI->profiles->currentText()+Core::TOX_EXT)); + bodyUI->profiles->removeItem(bodyUI->profiles->currentIndex()); + bodyUI->profiles->setCurrentText(Settings::getInstance().getCurrentProfile()); + } + } +} + +void IdentityForm::onImportClicked() +{ + QString path = QFileDialog::getOpenFileName(this, tr("Import profile", "import dialog title"), QDir::homePath(), tr("Tox save file (*.tox)", "import dialog filter")); + QFileInfo info(path); + QString profile = info.completeBaseName(); + QString profilePath = QDir(Settings::getSettingsDirPath()).filePath(profile + Core::TOX_EXT); + QFile::copy(path, profilePath); + bodyUI->profiles->addItem(profile); + Core::getInstance()->switchConfiguration(profile); +} diff --git a/widget/form/settings/identityform.h b/widget/form/settings/identityform.h index 2a4b07a77..a313aa135 100644 --- a/widget/form/settings/identityform.h +++ b/widget/form/settings/identityform.h @@ -60,6 +60,11 @@ private slots: void copyIdClicked(); void onUserNameEdited(); void onStatusMessageEdited(); + void onLoadClicked(); + void onRenameClicked(); + void onExportClicked(); + void onDeleteClicked(); + void onImportClicked(); private: Ui::IdentitySettings* bodyUI; diff --git a/widget/form/settings/identitysettings.ui b/widget/form/settings/identitysettings.ui index e68b99deb..47cb70f2e 100644 --- a/widget/form/settings/identitysettings.ui +++ b/widget/form/settings/identitysettings.ui @@ -75,7 +75,7 @@ - + diff --git a/widget/friendlistwidget.cpp b/widget/friendlistwidget.cpp index feb8ffa26..176850e82 100644 --- a/widget/friendlistwidget.cpp +++ b/widget/friendlistwidget.cpp @@ -66,22 +66,3 @@ void FriendListWidget::moveWidget(QWidget *w, Status s) mainLayout->removeWidget(w); getFriendLayout(s)->addWidget(w); } - -void clearLayout(QLayout *layout) -{ - QLayoutItem *item; - while((item = layout->takeAt(0))) - { - if (item->layout()) { - clearLayout(item->layout()); - delete item->layout(); - } - delete item->widget(); - delete item; - } -} - -void FriendListWidget::clear() -{ - clearLayout(mainLayout); -} diff --git a/widget/friendlistwidget.h b/widget/friendlistwidget.h index 2dd3d4fcd..f8c3adf8c 100644 --- a/widget/friendlistwidget.h +++ b/widget/friendlistwidget.h @@ -38,7 +38,6 @@ public: signals: public slots: - void clear(); private: QHash layouts; diff --git a/widget/widget.cpp b/widget/widget.cpp index 98fdad678..09aa0f8e0 100644 --- a/widget/widget.cpp +++ b/widget/widget.cpp @@ -137,7 +137,6 @@ Widget::Widget(QWidget *parent) connect(core, SIGNAL(fileUploadFinished(const QString&)), &filesForm, SLOT(onFileUploadComplete(const QString&))); connect(core, &Core::friendAdded, this, &Widget::addFriend); connect(core, &Core::failedToAddFriend, this, &Widget::addFriendFailed); - connect(core, &Core::clearFriends, contactListWidget, &FriendListWidget::clear); connect(core, &Core::friendStatusChanged, this, &Widget::onFriendStatusChanged); connect(core, &Core::friendUsernameChanged, this, &Widget::onFriendUsernameChanged); connect(core, &Core::friendStatusChanged, this, &Widget::onFriendStatusChanged); @@ -637,6 +636,14 @@ void Widget::removeFriend(int friendId) removeFriend(FriendList::findFriend(friendId)); } +void Widget::clearContactsList() +{ + for (Friend* f : FriendList::friendList) + removeFriend(f); + for (Group* g : GroupList::groupList) + removeGroup(g); +} + void Widget::copyFriendIdToClipboard(int friendId) { Friend* f = FriendList::findFriend(friendId); @@ -700,19 +707,23 @@ void Widget::onGroupNamelistChanged(int groupnumber, int peernumber, uint8_t Cha g->updatePeer(peernumber,core->getGroupPeerName(groupnumber, peernumber)); } -void Widget::removeGroup(int groupId) +void Widget::removeGroup(Group* g) { - Group* g = GroupList::findGroup(groupId); g->widget->setAsInactiveChatroom(); if (static_cast(g->widget) == activeChatroomWidget) activeChatroomWidget = nullptr; - GroupList::removeGroup(groupId); - core->removeGroup(groupId); + GroupList::removeGroup(g->groupId); + core->removeGroup(g->groupId); delete g; if (ui->mainHead->layout()->isEmpty()) onAddClicked(); } +void Widget::removeGroup(int groupId) +{ + removeGroup(GroupList::findGroup(groupId)); +} + Core *Widget::getCore() { return core; diff --git a/widget/widget.h b/widget/widget.h index 2d86fa7d9..a7f210d98 100644 --- a/widget/widget.h +++ b/widget/widget.h @@ -57,6 +57,7 @@ public: bool isFriendWidgetCurActiveWidget(Friend* f); bool getIsWindowMinimized(); static QList searchProfiles(); + void clearContactsList(); ~Widget(); virtual void closeEvent(QCloseEvent *event); @@ -112,6 +113,7 @@ private: virtual bool event(QEvent * e); Group* createGroup(int groupId); void removeFriend(Friend* f); + void removeGroup(Group* g); QString askProfiles(); QString detectProfile();