From e3da9badace3af72a04427a9e23490ea9b2a2cd7 Mon Sep 17 00:00:00 2001 From: dubslow Date: Wed, 16 Jul 2014 17:43:30 -0500 Subject: [PATCH] Basic infrastructure for profiles is in place, now I gotta hook up the UI --- core.cpp | 67 ++++++++++++++++++++++++++---------- core.h | 7 ++-- settings.cpp | 12 +++++++ settings.h | 4 +++ widget/form/settingsform.cpp | 46 ++++++++++++++++++++----- widget/form/settingsform.h | 15 +++++--- widget/widget.cpp | 36 ++++++++++++------- widget/widget.h | 4 +-- 8 files changed, 144 insertions(+), 47 deletions(-) diff --git a/core.cpp b/core.cpp index 2038131c0..1dcc9349c 100644 --- a/core.cpp +++ b/core.cpp @@ -30,6 +30,7 @@ #include const QString Core::CONFIG_FILE_NAME = "data"; +const QString Core::TOX_EXT = ".tox"; QList Core::fileSendQueue; QList Core::fileRecvQueue; ToxCall Core::calls[TOXAV_MAX_CALLS]; @@ -108,6 +109,7 @@ void Core::start() return; } + // this will attempt to get the last profile from settings loadConfiguration(); tox_callback_friend_request(tox, onFriendRequest, this); @@ -721,34 +723,55 @@ void Core::checkConnection() } } -void Core::loadConfiguration() +QString sanitize(QString name) { - QString path = Settings::getSettingsDirPath() + '/' + CONFIG_FILE_NAME; + // do things + return name; +} - QFile configurationFile(path); +void Core::loadConfiguration(QString path) +{ + if (path == "") + { + // read from settings whose profile? + path = Settings::getSettingsDirPath() + '/' + Settings::getInstance().getCurrentProfile() + TOX_EXT; + QFile file(path); + + // if the last profile doesn't exist, fall back to old "data" + if (!file.exists()) + { + path = Settings::getSettingsDirPath() + '/' + CONFIG_FILE_NAME; + } + } - if (!configurationFile.exists()) { + QFile conf(path); + qDebug() << "Core::loadConfiguration: reading from " << path; + + if (!conf.exists()) { qWarning() << "The Tox configuration file was not found"; return; } - if (!configurationFile.open(QIODevice::ReadOnly)) { + if (!conf.open(QIODevice::ReadOnly)) { qCritical() << "File " << path << " cannot be opened"; return; } - qint64 fileSize = configurationFile.size(); + qint64 fileSize = conf.size(); if (fileSize > 0) { - QByteArray data = configurationFile.readAll(); + QByteArray data = conf.readAll(); tox_load(tox, reinterpret_cast(data.data()), data.size()); } - configurationFile.close(); + conf.close(); // set GUI with user and statusmsg QString name = getUsername(); if (name != "") + { emit usernameSet(name); + Settings::getInstance().setCurrentProfile(name); + } QString msg = getStatusMessage(); if (msg != "") @@ -757,7 +780,7 @@ void Core::loadConfiguration() loadFriends(); } -void Core::saveConfiguration() +void Core::saveConfiguration(QString path) { if (!tox) { @@ -765,23 +788,28 @@ void Core::saveConfiguration() return; } - QString path = Settings::getSettingsDirPath(); - - QDir directory(path); - - if (!directory.exists() && !directory.mkpath(directory.absolutePath())) { - qCritical() << "Error while creating directory " << path; - return; + if (path == "") + { + QString dir = Settings::getSettingsDirPath(); + QDir directory(dir); + if (!directory.exists() && !directory.mkpath(directory.absolutePath())) { + qCritical() << "Error while creating directory " << dir; + return; + } + + path = dir + Settings::getInstance().getCurrentProfile() + TOX_EXT; + QFileInfo info(dir); + if (!info.exists()) // fall back to old school 'data' + path = dir + '/' + CONFIG_FILE_NAME; } - - path += '/' + CONFIG_FILE_NAME; + QSaveFile configurationFile(path); if (!configurationFile.open(QIODevice::WriteOnly)) { qCritical() << "File " << path << " cannot be opened"; return; } - qDebug() << "Core: writing tox_save"; + qDebug() << "Core: writing tox_save to " << path; uint32_t fileSize = tox_size(tox); if (fileSize > 0 && fileSize <= INT32_MAX) { uint8_t *data = new uint8_t[fileSize]; @@ -789,6 +817,7 @@ void Core::saveConfiguration() configurationFile.write(reinterpret_cast(data), fileSize); configurationFile.commit(); delete[] data; + //configurationFile.close(); } } diff --git a/core.h b/core.h index 91bbee5e2..af8d4e47e 100644 --- a/core.h +++ b/core.h @@ -114,6 +114,8 @@ class Core : public QObject public: explicit Core(Camera* cam, QThread* coreThread); ~Core(); + + static const QString TOX_EXT; int getGroupNumberPeers(int groupId) const; QString getGroupPeerName(int groupId, int peerId) const; @@ -122,7 +124,9 @@ public: void quitGroupChat(int groupId) const; void dispatchVideoFrame(vpx_image img) const; - void saveConfiguration(); + void saveConfiguration(QString path = ""); + void loadConfiguration(QString path = ""); + QString sanitize(QString name); QString getUsername(); QString getStatusMessage(); @@ -273,7 +277,6 @@ private: void checkConnection(); void onBootstrapTimer(); - void loadConfiguration(); void loadFriends(); static void sendAllFileData(Core* core, ToxFile* file); diff --git a/settings.cpp b/settings.cpp index 888d840a5..62a208df6 100644 --- a/settings.cpp +++ b/settings.cpp @@ -81,6 +81,7 @@ void Settings::load() enableIPv6 = s.value("enableIPv6", true).toBool(); useTranslations = s.value("useTranslations", true).toBool(); makeToxPortable = s.value("makeToxPortable", false).toBool(); + currentProfile = s.value("currentProfile", ".data").toString(); s.endGroup(); s.beginGroup("Widgets"); @@ -137,6 +138,7 @@ void Settings::save(QString path) s.setValue("enableIPv6", enableIPv6); s.setValue("useTranslations",useTranslations); s.setValue("makeToxPortable",makeToxPortable); + s.setValue("currentProfile", currentProfile); s.endGroup(); s.beginGroup("Widgets"); @@ -208,6 +210,16 @@ void Settings::setMakeToxPortable(bool newValue) save(FILENAME); // Commit to the portable file that we don't want to use it } +QString Settings::getCurrentProfile() const +{ + return currentProfile; +} + +void Settings::setCurrentProfile(QString profile) +{ + currentProfile = profile; +} + bool Settings::getUseTranslations() const { return useTranslations; diff --git a/settings.h b/settings.h index d4f015635..3ad240bc7 100644 --- a/settings.h +++ b/settings.h @@ -49,6 +49,9 @@ public: bool getMakeToxPortable() const; void setMakeToxPortable(bool newValue); + QString getCurrentProfile() const; + void setCurrentProfile(QString profile); + bool getUseTranslations() const; void setUseTranslations(bool newValue); @@ -134,6 +137,7 @@ private: bool enableIPv6; bool useTranslations; static bool makeToxPortable; + QString currentProfile; bool enableLogging; bool encryptLogs; diff --git a/widget/form/settingsform.cpp b/widget/form/settingsform.cpp index 1179497c5..b0815825d 100644 --- a/widget/form/settingsform.cpp +++ b/widget/form/settingsform.cpp @@ -25,14 +25,16 @@ SettingsForm::SettingsForm() : QObject() { main = new QWidget(), head = new QWidget(); + hboxcont1 = new QWidget(), hboxcont2 = new QWidget(); QFont bold, small; bold.setBold(true); small.setPixelSize(13); headLabel.setText(tr("User Settings","\"Headline\" of the window")); headLabel.setFont(bold); - nameLabel.setText(tr("Name","Username/nick")); - statusTextLabel.setText(tr("Status","Status message")); + //nameLabel.setText(tr("Name","Username/nick")); + //statusTextLabel.setText(tr("Status","Status message")); + idLabel.setText("Tox ID " + tr("(click here to copy)", "Click on this text to copy TID to clipboard")); id.setFont(small); id.setTextInteractionFlags(Qt::TextSelectableByMouse); @@ -40,6 +42,15 @@ SettingsForm::SettingsForm() id.setFrameStyle(QFrame::NoFrame); id.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); id.setFixedHeight(id.document()->size().height()); + + profilesLabel.setText(tr("Available profiles:", "Labels the profile selection box")); + populateProfiles(); + loadConf.setText(tr("Load profile", "button to load selected profile")); + exportConf.setText(tr("Export profile", "button to save selected profile elsewhere")); + delConf.setText(tr("Delete profile", "button to delete selected profile from disk")); + delConf.setToolTip(tr("This is useful to remain safe on public computers", "describes the delete profile button")); + //delConf.setWhatsThis(tr("This is useful to remain safe on public computers", "describes the delete profile button")); + importConf.setText(tr("Import profile", "button to locate a profile")); videoTest.setText(tr("Test video","Text on a button to test the video/webcam")); enableIPv6.setText(tr("Enable IPv6 (recommended)","Text on a checkbox to enable IPv6")); @@ -50,12 +61,21 @@ SettingsForm::SettingsForm() makeToxPortable.setChecked(Settings::getInstance().getMakeToxPortable()); main->setLayout(&layout); - layout.addWidget(&nameLabel); - layout.addWidget(&name); - layout.addWidget(&statusTextLabel); - layout.addWidget(&statusText); + //layout.addWidget(&nameLabel); + //layout.addWidget(&name); + //layout.addWidget(&statusTextLabel); + //layout.addWidget(&statusText); layout.addWidget(&idLabel); layout.addWidget(&id); + cbox.addWidget(&profilesLabel); + cbox.addWidget(&profiles); + hboxcont1->setLayout(&cbox); + layout.addWidget(hboxcont1); + buttons.addWidget(&loadConf); + buttons.addWidget(&exportConf); + buttons.addWidget(&delConf); + hboxcont2->setLayout(&buttons); + layout.addWidget(hboxcont2); layout.addWidget(&videoTest); layout.addWidget(&enableIPv6); layout.addWidget(&useTranslations); @@ -76,6 +96,16 @@ SettingsForm::~SettingsForm() { } +void SettingsForm::populateProfiles() +{ + QDir dir(Settings::getSettingsDirPath()); + dir.setFilter(QDir::Files | QDir::NoDotAndDotDot); + dir.setNameFilters(QStringList("*.tox")); + for(QFileInfo file : dir.entryInfoList()) { + profiles.addItem(file.completeBaseName()); + } +} + void SettingsForm::setFriendAddress(const QString& friendAddress) { id.setText(friendAddress); @@ -83,8 +113,8 @@ void SettingsForm::setFriendAddress(const QString& friendAddress) void SettingsForm::show(Ui::Widget &ui) { - name.setText(ui.nameLabel->text()); - statusText.setText(ui.statusLabel->text()); + //name.setText(ui.nameLabel->text()); + //statusText.setText(ui.statusLabel->text()); ui.mainContent->layout()->addWidget(main); ui.mainHead->layout()->addWidget(head); main->show(); diff --git a/widget/form/settingsform.h b/widget/form/settingsform.h index 3342c5667..070c4e1b4 100644 --- a/widget/form/settingsform.h +++ b/widget/form/settingsform.h @@ -26,6 +26,9 @@ #include #include #include +#include +#include +#include #include "widget/tool/clickablelabel.h" #include "ui_widget.h" #include "widget/selfcamview.h" @@ -50,16 +53,20 @@ private slots: void copyIdClicked(); private: - QLabel headLabel, nameLabel, statusTextLabel; + QLabel headLabel;/*, nameLabel, statusTextLabel;*/ QTextEdit id; ClickableLabel idLabel; - QPushButton videoTest; + QLabel profilesLabel; + QComboBox profiles; + QPushButton loadConf, exportConf, delConf, importConf, videoTest; + QHBoxLayout cbox, buttons; QCheckBox enableIPv6, useTranslations, makeToxPortable; QVBoxLayout layout, headLayout; - QWidget *main, *head; + QWidget *main, *head, *hboxcont1, *hboxcont2; + void populateProfiles(); public: - QLineEdit name, statusText; + //QLineEdit name, statusText; }; #endif // SETTINGSFORM_H diff --git a/widget/widget.cpp b/widget/widget.cpp index f7b7bc1b3..468ea3b83 100644 --- a/widget/widget.cpp +++ b/widget/widget.cpp @@ -249,8 +249,8 @@ Widget::Widget(QWidget *parent) : connect(setStatusOnline, SIGNAL(triggered()), this, SLOT(setStatusOnline())); connect(setStatusAway, SIGNAL(triggered()), this, SLOT(setStatusAway())); connect(setStatusBusy, SIGNAL(triggered()), this, SLOT(setStatusBusy())); - connect(&settingsForm.name, SIGNAL(editingFinished()), this, SLOT(onUsernameChanged())); - connect(&settingsForm.statusText, SIGNAL(editingFinished()), this, SLOT(onStatusMessageChanged())); + //connect(&settingsForm.name, SIGNAL(editingFinished()), this, SLOT(onUsernameChanged())); + //connect(&settingsForm.statusText, SIGNAL(editingFinished()), this, SLOT(onStatusMessageChanged())); connect(&friendForm, SIGNAL(friendRequested(QString,QString)), this, SIGNAL(friendRequested(QString,QString))); coreThread->start(); @@ -418,44 +418,56 @@ void Widget::hideMainForms() } } -void Widget::onUsernameChanged() +/*void Widget::onUsernameChanged() { - const QString newUsername = settingsForm.name.text(); + //const QString newUsername = settingsForm.name.text(); ui->nameLabel->setText(newUsername); ui->nameLabel->setToolTip(newUsername); // for overlength names settingsForm.name.setText(newUsername); core->setUsername(newUsername); -} +}*/ void Widget::onUsernameChanged(const QString& newUsername, const QString& oldUsername) { ui->nameLabel->setText(oldUsername); // restore old username until Core tells us to set it ui->nameLabel->setToolTip(oldUsername); // for overlength names - settingsForm.name.setText(oldUsername); + //settingsForm.name.setText(oldUsername); core->setUsername(newUsername); + + // move the data file with it + QString dir = Settings::getSettingsDirPath(); + QFile::rename(dir + '/' + oldUsername + core->TOX_EXT, dir + '/' + newUsername + core->TOX_EXT); + // and update current profile + Settings::getInstance().setCurrentProfile(newUsername); } - +// ugh... Widget::onUsernameChanged() calls Core::setUsername, +// which emits Core::usernameSet, which is connect to this function: void Widget::setUsername(const QString& username) { ui->nameLabel->setText(username); ui->nameLabel->setToolTip(username); // for overlength names - settingsForm.name.setText(username); + //settingsForm.name.setText(username); } +// the end result is that the ui gets updated twice -- and actually, +// I believe this function is dead code, since the only way to change +// username now is via the ui (since I killed the settings version) +// the chain of calls before was surely much messier with two ways +// to modify the username... -void Widget::onStatusMessageChanged() +/*void Widget::onStatusMessageChanged() { const QString newStatusMessage = settingsForm.statusText.text(); ui->statusLabel->setText(newStatusMessage); ui->statusLabel->setToolTip(newStatusMessage); // for overlength messsages settingsForm.statusText.setText(newStatusMessage); core->setStatusMessage(newStatusMessage); -} +}*/ void Widget::onStatusMessageChanged(const QString& newStatusMessage, const QString& oldStatusMessage) { ui->statusLabel->setText(oldStatusMessage); // restore old status message until Core tells us to set it ui->statusLabel->setToolTip(oldStatusMessage); // for overlength messsages - settingsForm.statusText.setText(oldStatusMessage); + //settingsForm.statusText.setText(oldStatusMessage); core->setStatusMessage(newStatusMessage); } @@ -463,7 +475,7 @@ void Widget::setStatusMessage(const QString &statusMessage) { ui->statusLabel->setText(statusMessage); ui->statusLabel->setToolTip(statusMessage); // for overlength messsages - settingsForm.statusText.setText(statusMessage); + //settingsForm.statusText.setText(statusMessage); } void Widget::addFriend(int friendId, const QString &userId) diff --git a/widget/widget.h b/widget/widget.h index 0e0a13109..42953e6e8 100644 --- a/widget/widget.h +++ b/widget/widget.h @@ -86,8 +86,8 @@ private slots: void onFailedToStartCore(); void onUsernameChanged(const QString& newUsername, const QString& oldUsername); void onStatusMessageChanged(const QString& newStatusMessage, const QString& oldStatusMessage); - void onUsernameChanged(); - void onStatusMessageChanged(); + //void onUsernameChanged(); + //void onStatusMessageChanged(); void setUsername(const QString& username); void setStatusMessage(const QString &statusMessage); void addFriend(int friendId, const QString& userId);