From a2bd266c113416a41564839835fe339dd76eabb9 Mon Sep 17 00:00:00 2001 From: LorenDB Date: Wed, 21 Oct 2020 19:20:02 -0400 Subject: [PATCH] Add option for unique config --- README.md | 1 + src/ChatPage.cpp | 16 ++------------- src/ChatPage.h | 3 +-- src/MainWindow.cpp | 22 +++++++++++++++++---- src/MainWindow.h | 6 +++++- src/UserSettingsPage.cpp | 3 +++ src/main.cpp | 42 +++++++++++++++++++++++++++++++++++++--- 7 files changed, 69 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 92b55460..ee42cb0c 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Specifically there is support for: - Basic communities support. - Room switcher (ctrl-K). - Light, Dark & System themes. +- Creating separate profiles (command line only, use `--profile=name`). ## Installation diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 8b6f1123..d5a7fe87 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -272,7 +272,7 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) connect(room_list_, SIGNAL(totalUnreadMessageCountUpdated(int)), this, - SLOT(showUnreadMessageNotification(int))); + SIGNAL(unreadMessages(int))); connect(text_input_, &TextInputWidget::sendTextMessage, @@ -626,7 +626,7 @@ ChatPage::resetUI() user_info_widget_->reset(); view_manager_->clearAll(); - showUnreadMessageNotification(0); + emit unreadMessages(0); } void @@ -751,18 +751,6 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token) tryInitialSync(); } -void -ChatPage::showUnreadMessageNotification(int count) -{ - emit unreadMessages(count); - - // TODO: Make the default title a const. - if (count == 0) - emit changeWindowTitle("nheko"); - else - emit changeWindowTitle(QString("nheko (%1)").arg(count)); -} - void ChatPage::loadStateFromCache() { diff --git a/src/ChatPage.h b/src/ChatPage.h index bf649cc9..a29cea7b 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -130,7 +130,7 @@ signals: void contentLoaded(); void closing(); - void changeWindowTitle(const QString &msg); + void changeWindowTitle(const int); void unreadMessages(int count); void showNotification(const QString &msg); void showLoginPage(const QString &msg); @@ -188,7 +188,6 @@ signals: void receivedDeviceVerificationDone(const mtx::events::msg::KeyVerificationDone &message); private slots: - void showUnreadMessageNotification(int count); void logout(); void removeRoom(const QString &room_id); void dropToLoginPage(const QString &msg); diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index b6ad8bbe..c019b24b 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -53,10 +53,11 @@ MainWindow *MainWindow::instance_ = nullptr; -MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent) +MainWindow::MainWindow(const QString profile, QWidget *parent) + : QMainWindow(parent), + profile_{ profile } { - setWindowTitle("nheko"); + setWindowTitle(0); setObjectName("MainWindow"); modal_ = new OverlayModal(this); @@ -104,7 +105,7 @@ MainWindow::MainWindow(QWidget *parent) connect( chat_page_, &ChatPage::showOverlayProgressBar, this, &MainWindow::showOverlayProgressBar); connect( - chat_page_, SIGNAL(changeWindowTitle(QString)), this, SLOT(setWindowTitle(QString))); + chat_page_, &ChatPage::unreadMessages, this, &MainWindow::setWindowTitle); connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int))); connect(chat_page_, &ChatPage::showLoginPage, this, [this](const QString &msg) { login_page_->loginError(msg); @@ -178,6 +179,19 @@ MainWindow::MainWindow(QWidget *parent) } } +void +MainWindow::setWindowTitle(int notificationCount) +{ + QString name = "nheko"; + if (!profile_.isEmpty()) + name += " | " + profile_; + if (notificationCount > 0) + { + name.append(QString{" (%1)"}.arg(notificationCount)); + } + QMainWindow::setWindowTitle(name); +} + void MainWindow::showEvent(QShowEvent *event) { diff --git a/src/MainWindow.h b/src/MainWindow.h index e66f299f..2f9ff897 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -62,7 +62,7 @@ class MainWindow : public QMainWindow Q_OBJECT public: - explicit MainWindow(QWidget *parent = nullptr); + explicit MainWindow(const QString name, QWidget *parent = nullptr); static MainWindow *instance() { return instance_; }; void saveCurrentWindowSize(); @@ -113,6 +113,8 @@ private slots: void showOverlayProgressBar(); void removeOverlayProgressBar(); + virtual void setWindowTitle(int notificationCount); + private: bool loadJdenticonPlugin(); @@ -147,4 +149,6 @@ private: LoadingIndicator *spinner_ = nullptr; JdenticonInterface *jdenticonInteface_ = nullptr; + + QString profile_; }; diff --git a/src/UserSettingsPage.cpp b/src/UserSettingsPage.cpp index 4278180b..059fcb47 100644 --- a/src/UserSettingsPage.cpp +++ b/src/UserSettingsPage.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include "Cache.h" #include "Config.h" @@ -433,6 +434,8 @@ UserSettingsPage::UserSettingsPage(QSharedPointer settings, QWidge font.setPointSizeF(font.pointSizeF() * 1.1); auto versionInfo = new QLabel(QString("%1 | %2").arg(nheko::version).arg(nheko::build_os)); + if (QCoreApplication::applicationName() != "nheko") + versionInfo->setText(versionInfo->text() + " | " + tr("profile: %1").arg(QCoreApplication::applicationName())); versionInfo->setTextInteractionFlags(Qt::TextBrowserInteraction); topBarLayout_ = new QHBoxLayout; diff --git a/src/main.cpp b/src/main.cpp index e02ffa36..61cb4fbe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -104,10 +104,37 @@ createCacheDirectory() int main(int argc, char *argv[]) { - // needed for settings so need to register before any settings are read to prevent warings + // needed for settings so need to register before any settings are read to prevent warnings qRegisterMetaType(); - QCoreApplication::setApplicationName("nheko"); + // This is some hacky programming, but it's necessary (AFAIK?) to get the unique config name parsed + // before the app name is set. + QString appName{"nheko"}; + for (int i = 0; i < argc; ++i) + { + if (QString{argv[i]}.startsWith("--profile=")) + { + QString q{argv[i]}; + q.remove("--profile="); + appName += "-" + q; + } + else if (QString{argv[i]}.startsWith("--p=")) + { + QString q{argv[i]}; + q.remove("-p="); + appName += "-" + q; + } + else if (QString{argv[i]} == "--profile" || QString{argv[i]} == "-p") + { + if (i < argc -1) // if i is less than argc - 1, we still have a parameter left to process as the name + { + ++i; // the next arg is the name, so increment + appName += "-" + QString {argv[i]}; + } + } + } + + QCoreApplication::setApplicationName(appName); QCoreApplication::setApplicationVersion(nheko::version); QCoreApplication::setOrganizationName("nheko"); QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings); @@ -137,6 +164,15 @@ main(int argc, char *argv[]) parser.addVersionOption(); QCommandLineOption debugOption("debug", "Enable debug output"); parser.addOption(debugOption); + + // This option is not actually parsed via Qt due to the need to parse it before the app + // name is set. It only exists to keep Qt from complaining about the --profile/-p + // option and thereby crashing the app. + QCommandLineOption configName(QStringList() << "p" << "profile", + QCoreApplication::tr("Create a unique profile, which allows you to log into several accounts at the same time and start multiple instances of nheko."), + QCoreApplication::tr("profile"), QCoreApplication::tr("profile name")); + parser.addOption(configName); + parser.process(app); app.setWindowIcon(QIcon(":/logos/nheko.png")); @@ -181,7 +217,7 @@ main(int argc, char *argv[]) appTranslator.load(QLocale(), "nheko", "_", ":/translations"); app.installTranslator(&appTranslator); - MainWindow w; + MainWindow w{ (appName == "nheko" ? "" : appName.remove("nheko-")) }; // Move the MainWindow to the center w.move(screenCenter(w.width(), w.height()));