From 2181313d37daa7b8d6da521ab1946fe2de00a7de Mon Sep 17 00:00:00 2001 From: dubslow Date: Thu, 9 Oct 2014 00:30:33 -0500 Subject: [PATCH] add startup profile detection --- core.cpp | 30 +++++++----------------- core.h | 7 +++--- widget/widget.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++-- widget/widget.h | 4 +++- 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/core.cpp b/core.cpp index 6b899ab8b..7c97d3387 100644 --- a/core.cpp +++ b/core.cpp @@ -43,8 +43,8 @@ const QString Core::TOX_EXT = ".tox"; QList Core::fileSendQueue; QList Core::fileRecvQueue; -Core::Core(Camera* cam, QThread *coreThread) : - tox(nullptr), camera(cam) +Core::Core(Camera* cam, QThread *coreThread, QString loadPath) : + tox(nullptr), camera(cam), loadPath(loadPath) { videobuf = new uint8_t[videobufsize]; videoBusyness=0; @@ -213,28 +213,14 @@ void Core::start() qsrand(time(nullptr)); - // where do we find the data file? - QString path; - { // read data from whose profile? - path = Settings::getSettingsDirPath() + QDir::separator() + Settings::getInstance().getCurrentProfile() + TOX_EXT; - -#if 1 // deprecation attempt - // if the last profile doesn't exist, fall back to old "data" - //! or maybe, should we give an option to choose other existing profiles? - QFile file(path); - if (!file.exists()) - { - path = Settings::getSettingsDirPath() + QDir::separator() + CONFIG_FILE_NAME; - } -#endif - } - if (!loadConfiguration(path)) + if (!loadConfiguration(loadPath)) // loadPath is meaningless after this { emit failedToStart(); tox_kill(tox); tox = nullptr; return; } + loadPath = ""; tox_callback_friend_request(tox, onFriendRequest, this); tox_callback_friend_message(tox, onFriendMessage, this); @@ -1156,10 +1142,10 @@ void Core::saveConfiguration() QString path = dir + QDir::separator() + profile + TOX_EXT; QFileInfo info(path); - if (!info.exists()) // fall back to old school 'data' - { //path = dir + QDir::separator() + CONFIG_FILE_NAME; - qDebug() << path << " does not exist"; - } +// if (!info.exists()) // fall back to old school 'data' +// { //path = dir + QDir::separator() + CONFIG_FILE_NAME; +// qDebug() << "Core:" << path << " does not exist"; +// } saveConfiguration(path); } diff --git a/core.h b/core.h index 292c82494..7062072d3 100644 --- a/core.h +++ b/core.h @@ -34,11 +34,12 @@ class Core : public QObject { Q_OBJECT public: - explicit Core(Camera* cam, QThread* coreThread); + explicit Core(Camera* cam, QThread* coreThread, QString initialLoadPath); static Core* getInstance(); ///< Returns the global widget's Core instance ~Core(); static const QString TOX_EXT; + static const QString CONFIG_FILE_NAME; int getGroupNumberPeers(int groupId) const; QString getGroupPeerName(int groupId, int peerId) const; @@ -234,14 +235,14 @@ private slots: private: Tox* tox; ToxAv* toxav; - QTimer *toxTimer, *fileTimer, *bootstrapTimer; //, *saveTimer; + QTimer *toxTimer, *fileTimer; //, *saveTimer; Camera* camera; + QString loadPath; // meaningless after start() is called QList dhtServerList; int dhtServerId; static QList fileSendQueue, fileRecvQueue; static ToxCall calls[]; - static const QString CONFIG_FILE_NAME; static const int videobufsize; static uint8_t* videobuf; static int videoBusyness; // Used to know when to drop frames diff --git a/widget/widget.cpp b/widget/widget.cpp index f610e8187..a3b07bc1d 100644 --- a/widget/widget.cpp +++ b/widget/widget.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include Widget *Widget::instance{nullptr}; @@ -118,9 +119,9 @@ Widget::Widget(QWidget *parent) qRegisterMetaType("ToxFile"); qRegisterMetaType("ToxFile::FileDirection"); - // QString path = detectProfiles(); + QString profilePath = detectProfile(); coreThread = new QThread(this); - core = new Core(camera, coreThread/*, profile*/); + core = new Core(camera, coreThread, profilePath); core->moveToThread(coreThread); connect(coreThread, &QThread::started, core, &Core::start); @@ -215,6 +216,59 @@ void Widget::closeEvent(QCloseEvent *event) QWidget::closeEvent(event); } +QString Widget::detectProfile() +{ + QDir dir(Settings::getSettingsDirPath()); + QString path, profile = Settings::getInstance().getCurrentProfile(); + path = dir.filePath(profile + Core::TOX_EXT); + QFile file(path); + if (profile == "" || !file.exists()) + { +#if 1 // deprecation attempt + // if the last profile doesn't exist, fall back to old "data" + path = dir.filePath(Core::CONFIG_FILE_NAME); + QFile file(path); + if (file.exists()) + return path; + else +#endif + return dir.filePath(askProfiles() + Core::TOX_EXT); + } + else + return path; +} + +QList Widget::searchProfiles() +{ + QList out; + QDir dir(Settings::getSettingsDirPath()); + dir.setFilter(QDir::Files | QDir::NoDotAndDotDot); + dir.setNameFilters(QStringList("*.tox")); + for(QFileInfo file : dir.entryInfoList()) + out += file.completeBaseName(); + return out; +} + +QString Widget::askProfiles() +{ + QList profiles = searchProfiles(); + bool ok; + QString profile = QInputDialog::getItem(this, + tr("Choose a profile"), + tr("Please choose which identity to use"), + profiles, + 0, // which slot to start on + false, // if the user can enter their own input + &ok); + if (!ok) // user cancelled + { + qApp->quit(); + return ""; + } + else + return profile; +} + QString Widget::getUsername() { return core->getUsername(); diff --git a/widget/widget.h b/widget/widget.h index 115fccab0..b93b224e1 100644 --- a/widget/widget.h +++ b/widget/widget.h @@ -56,6 +56,7 @@ public: void newMessageAlert(); bool isFriendWidgetCurActiveWidget(Friend* f); bool getIsWindowMinimized(); + static QList searchProfiles(); ~Widget(); virtual void closeEvent(QCloseEvent *event); @@ -112,8 +113,9 @@ private: virtual bool event(QEvent * e); Group* createGroup(int groupId); void removeFriend(Friend* f); + QString askProfiles(); + QString detectProfile(); -private: Ui::MainWindow *ui; QSplitter *centralLayout; QPoint dragPosition;