From 981ef30ab4a336a3ed3cfa4db74299bb08ab8b59 Mon Sep 17 00:00:00 2001 From: Mike Gelfand Date: Mon, 3 Oct 2016 21:26:28 +0300 Subject: [PATCH] Use session ID (if available) to check if session is local or not (Qt client) --- qt/OptionsDialog.cc | 72 ++++++++++++++++++++++++------------------ qt/OptionsDialog.h | 4 +++ qt/PrefsDialog.cc | 77 ++++++++++++++++++++++++--------------------- qt/PrefsDialog.h | 5 ++- qt/Session.cc | 20 +++++++++++- qt/Session.h | 2 ++ 6 files changed, 113 insertions(+), 67 deletions(-) diff --git a/qt/OptionsDialog.cc b/qt/OptionsDialog.cc index b9d35b477..22f48887e 100644 --- a/qt/OptionsDialog.cc +++ b/qt/OptionsDialog.cc @@ -29,8 +29,9 @@ OptionsDialog::OptionsDialog (Session& session, const Prefs& prefs, const AddDat BaseDialog (parent), mySession (session), myAdd (addme), + myIsLocal (mySession.isLocal ()), myHaveInfo (false), - myVerifyButton (nullptr), + myVerifyButton (new QPushButton (tr ("&Verify Local Data"), this)), myVerifyFile (nullptr), myVerifyHash (QCryptographicHash::Sha1), myEditTimer (this) @@ -76,42 +77,27 @@ OptionsDialog::OptionsDialog (Session& session, const Prefs& prefs, const AddDat ui.freeSpaceLabel->setSession (mySession); ui.freeSpaceLabel->setPath (downloadDir); - if (session.isLocal ()) - { - ui.destinationStack->setCurrentWidget (ui.destinationButton); - ui.destinationButton->setMode (PathButton::DirectoryMode); - ui.destinationButton->setTitle (tr ("Select Destination")); - ui.destinationButton->setPath (downloadDir); - myLocalDestination = downloadDir; - connect (ui.destinationButton, SIGNAL (pathChanged (QString)), this, SLOT (onDestinationChanged ())); - } - else - { - ui.destinationStack->setCurrentWidget (ui.destinationEdit); - ui.destinationEdit->setText (downloadDir); - ui.freeSpaceLabel->setPath (downloadDir); - connect (ui.destinationEdit, SIGNAL (textEdited (QString)), &myEditTimer, SLOT (start ())); - connect (ui.destinationEdit, SIGNAL (editingFinished ()), this, SLOT (onDestinationChanged ())); - } + ui.destinationButton->setMode (PathButton::DirectoryMode); + ui.destinationButton->setTitle (tr ("Select Destination")); + ui.destinationButton->setPath (downloadDir); + ui.destinationEdit->setText (downloadDir); - ui.destinationStack->setFixedHeight (ui.destinationStack->currentWidget ()->sizeHint ().height ()); - ui.destinationLabel->setBuddy (ui.destinationStack->currentWidget ()); + if (myIsLocal) + myLocalDestination = downloadDir; + + connect (ui.destinationButton, SIGNAL (pathChanged (QString)), this, SLOT (onDestinationChanged ())); + connect (ui.destinationEdit, SIGNAL (textEdited (QString)), &myEditTimer, SLOT (start ())); + connect (ui.destinationEdit, SIGNAL (editingFinished ()), this, SLOT (onDestinationChanged ())); ui.filesView->setEditable (false); - if (!session.isLocal ()) - ui.filesView->hideColumn (2); // hide the % done, since we've no way of knowing ui.priorityCombo->addItem (tr ("High"), TR_PRI_HIGH); ui.priorityCombo->addItem (tr ("Normal"), TR_PRI_NORMAL); ui.priorityCombo->addItem (tr ("Low"), TR_PRI_LOW); ui.priorityCombo->setCurrentIndex (1); // Normal - if (session.isLocal ()) - { - myVerifyButton = new QPushButton (tr ("&Verify Local Data"), this); - ui.dialogButtons->addButton (myVerifyButton, QDialogButtonBox::ActionRole); - connect (myVerifyButton, SIGNAL (clicked (bool)), this, SLOT (onVerify ())); - } + ui.dialogButtons->addButton (myVerifyButton, QDialogButtonBox::ActionRole); + connect (myVerifyButton, SIGNAL (clicked (bool)), this, SLOT (onVerify ())); ui.startCheck->setChecked (prefs.getBool (Prefs::START)); ui.trashCheck->setChecked (prefs.getBool (Prefs::TRASH_ORIGINAL)); @@ -124,6 +110,9 @@ OptionsDialog::OptionsDialog (Session& session, const Prefs& prefs, const AddDat connect (&myVerifyTimer, SIGNAL (timeout ()), this, SLOT (onTimeout ())); + connect (&mySession, SIGNAL (sessionUpdated ()), SLOT (onSessionUpdated ())); + + updateWidgetsLocality (); reload (); } @@ -184,8 +173,7 @@ OptionsDialog::reload () const bool haveFilesToShow = myHaveInfo && myInfo.fileCount > 0; ui.filesView->setVisible (haveFilesToShow); - if (myVerifyButton != nullptr) - myVerifyButton->setVisible (haveFilesToShow); + myVerifyButton->setEnabled (haveFilesToShow); layout ()->setSizeConstraint (haveFilesToShow ? QLayout::SetDefaultConstraint : QLayout::SetFixedSize); if (myHaveInfo) @@ -209,6 +197,30 @@ OptionsDialog::reload () ui.filesView->update (myFiles); } +void +OptionsDialog::updateWidgetsLocality () +{ + ui.destinationStack->setCurrentWidget (myIsLocal ? static_cast (ui.destinationButton) : ui.destinationEdit); + ui.destinationStack->setFixedHeight (ui.destinationStack->currentWidget ()->sizeHint ().height ()); + ui.destinationLabel->setBuddy (ui.destinationStack->currentWidget ()); + + // hide the % done when non-local, since we've no way of knowing + (ui.filesView->*(myIsLocal ? &QTreeView::showColumn : &QTreeView::hideColumn)) (2); + + myVerifyButton->setVisible (myIsLocal); +} + +void +OptionsDialog::onSessionUpdated () +{ + const bool isLocal = mySession.isLocal (); + if (myIsLocal != isLocal) + { + myIsLocal = isLocal; + updateWidgetsLocality (); + } +} + void OptionsDialog::onPriorityChanged (const QSet& fileIndices, int priority) { diff --git a/qt/OptionsDialog.h b/qt/OptionsDialog.h index 60fd77f6c..cd5b319c5 100644 --- a/qt/OptionsDialog.h +++ b/qt/OptionsDialog.h @@ -43,6 +43,7 @@ class OptionsDialog: public BaseDialog private: void reload (); + void updateWidgetsLocality (); void clearInfo (); void clearVerify (); @@ -56,12 +57,15 @@ class OptionsDialog: public BaseDialog void onSourceChanged (); void onDestinationChanged (); + void onSessionUpdated (); + private: Session& mySession; AddData myAdd; Ui::OptionsDialog ui; + bool myIsLocal; QDir myLocalDestination; bool myHaveInfo; tr_info myInfo; diff --git a/qt/PrefsDialog.cc b/qt/PrefsDialog.cc index adcaafe17..2d31d1059 100644 --- a/qt/PrefsDialog.cc +++ b/qt/PrefsDialog.cc @@ -467,56 +467,38 @@ PrefsDialog::onQueueStalledMinutesChanged () void PrefsDialog::initDownloadingTab () { - if (mySession.isLocal ()) - { - ui.watchDirStack->setCurrentWidget (ui.watchDirButton); - ui.downloadDirStack->setCurrentWidget (ui.downloadDirButton); - ui.incompleteDirStack->setCurrentWidget (ui.incompleteDirButton); - ui.completionScriptStack->setCurrentWidget (ui.completionScriptButton); - - ui.watchDirButton->setMode (PathButton::DirectoryMode); - ui.downloadDirButton->setMode (PathButton::DirectoryMode); - ui.incompleteDirButton->setMode (PathButton::DirectoryMode); - ui.completionScriptButton->setMode (PathButton::FileMode); - - ui.watchDirButton->setTitle (tr ("Select Watch Directory")); - ui.downloadDirButton->setTitle (tr ("Select Destination")); - ui.incompleteDirButton->setTitle (tr ("Select Incomplete Directory")); - ui.completionScriptButton->setTitle (tr ("Select \"Torrent Done\" Script")); - } - else - { - ui.watchDirStack->setCurrentWidget (ui.watchDirEdit); - ui.downloadDirStack->setCurrentWidget (ui.downloadDirEdit); - ui.incompleteDirStack->setCurrentWidget (ui.incompleteDirEdit); - ui.completionScriptStack->setCurrentWidget (ui.completionScriptEdit); - } + ui.watchDirButton->setMode (PathButton::DirectoryMode); + ui.downloadDirButton->setMode (PathButton::DirectoryMode); + ui.incompleteDirButton->setMode (PathButton::DirectoryMode); + ui.completionScriptButton->setMode (PathButton::FileMode); - ui.watchDirStack->setFixedHeight (ui.watchDirStack->currentWidget ()->sizeHint ().height ()); - ui.downloadDirStack->setFixedHeight (ui.downloadDirStack->currentWidget ()->sizeHint ().height ()); - ui.incompleteDirStack->setFixedHeight (ui.incompleteDirStack->currentWidget ()->sizeHint ().height ()); - ui.completionScriptStack->setFixedHeight (ui.completionScriptStack->currentWidget ()->sizeHint ().height ()); + ui.watchDirButton->setTitle (tr ("Select Watch Directory")); + ui.downloadDirButton->setTitle (tr ("Select Destination")); + ui.incompleteDirButton->setTitle (tr ("Select Incomplete Directory")); + ui.completionScriptButton->setTitle (tr ("Select \"Torrent Done\" Script")); ui.watchDirStack->setMinimumWidth (200); - ui.downloadDirLabel->setBuddy (ui.downloadDirStack->currentWidget ()); - ui.downloadDirFreeSpaceLabel->setSession (mySession); ui.downloadDirFreeSpaceLabel->setPath (myPrefs.getString (Prefs::DOWNLOAD_DIR)); linkWidgetToPref (ui.watchDirCheck, Prefs::DIR_WATCH_ENABLED); - linkWidgetToPref (ui.watchDirStack->currentWidget (), Prefs::DIR_WATCH); + linkWidgetToPref (ui.watchDirButton, Prefs::DIR_WATCH); + linkWidgetToPref (ui.watchDirEdit, Prefs::DIR_WATCH); linkWidgetToPref (ui.showTorrentOptionsDialogCheck, Prefs::OPTIONS_PROMPT); linkWidgetToPref (ui.startAddedTorrentsCheck, Prefs::START); linkWidgetToPref (ui.trashTorrentFileCheck, Prefs::TRASH_ORIGINAL); - linkWidgetToPref (ui.downloadDirStack->currentWidget (), Prefs::DOWNLOAD_DIR); + linkWidgetToPref (ui.downloadDirButton, Prefs::DOWNLOAD_DIR); + linkWidgetToPref (ui.downloadDirEdit, Prefs::DOWNLOAD_DIR); linkWidgetToPref (ui.downloadQueueSizeSpin, Prefs::DOWNLOAD_QUEUE_SIZE); linkWidgetToPref (ui.queueStalledMinutesSpin, Prefs::QUEUE_STALLED_MINUTES); linkWidgetToPref (ui.renamePartialFilesCheck, Prefs::RENAME_PARTIAL_FILES); linkWidgetToPref (ui.incompleteDirCheck, Prefs::INCOMPLETE_DIR_ENABLED); - linkWidgetToPref (ui.incompleteDirStack->currentWidget (), Prefs::INCOMPLETE_DIR); + linkWidgetToPref (ui.incompleteDirButton, Prefs::INCOMPLETE_DIR); + linkWidgetToPref (ui.incompleteDirEdit, Prefs::INCOMPLETE_DIR); linkWidgetToPref (ui.completionScriptCheck, Prefs::SCRIPT_TORRENT_DONE_ENABLED); - linkWidgetToPref (ui.completionScriptStack->currentWidget (), Prefs::SCRIPT_TORRENT_DONE_FILENAME); + linkWidgetToPref (ui.completionScriptButton, Prefs::SCRIPT_TORRENT_DONE_FILENAME); + linkWidgetToPref (ui.completionScriptEdit, Prefs::SCRIPT_TORRENT_DONE_FILENAME); ColumnResizer * cr (new ColumnResizer (this)); cr->addLayout (ui.addingSectionLayout); @@ -526,9 +508,26 @@ PrefsDialog::initDownloadingTab () connect (ui.queueStalledMinutesSpin, SIGNAL (valueChanged (int)), SLOT (onQueueStalledMinutesChanged ())); + updateDownloadingWidgetsLocality (); onQueueStalledMinutesChanged (); } +void +PrefsDialog::updateDownloadingWidgetsLocality () +{ + ui.watchDirStack->setCurrentWidget (myIsLocal ? static_cast (ui.watchDirButton) : ui.watchDirEdit); + ui.downloadDirStack->setCurrentWidget (myIsLocal ? static_cast (ui.downloadDirButton) : ui.downloadDirEdit); + ui.incompleteDirStack->setCurrentWidget (myIsLocal ? static_cast (ui.incompleteDirButton) : ui.incompleteDirEdit); + ui.completionScriptStack->setCurrentWidget (myIsLocal ? static_cast (ui.completionScriptButton) : ui.completionScriptEdit); + + ui.watchDirStack->setFixedHeight (ui.watchDirStack->currentWidget ()->sizeHint ().height ()); + ui.downloadDirStack->setFixedHeight (ui.downloadDirStack->currentWidget ()->sizeHint ().height ()); + ui.incompleteDirStack->setFixedHeight (ui.incompleteDirStack->currentWidget ()->sizeHint ().height ()); + ui.completionScriptStack->setFixedHeight (ui.completionScriptStack->currentWidget ()->sizeHint ().height ()); + + ui.downloadDirLabel->setBuddy (ui.downloadDirStack->currentWidget ()); +} + /*** **** ***/ @@ -537,7 +536,8 @@ PrefsDialog::PrefsDialog (Session& session, Prefs& prefs, QWidget * parent): BaseDialog (parent), mySession (session), myPrefs (prefs), - myIsServer (session.isServer ()) + myIsServer (session.isServer ()), + myIsLocal (mySession.isLocal ()) { ui.setupUi (this); @@ -597,6 +597,13 @@ PrefsDialog::setPref (int key, const QVariant& v) void PrefsDialog::sessionUpdated () { + const bool isLocal = mySession.isLocal (); + if (myIsLocal != isLocal) + { + myIsLocal = isLocal; + updateDownloadingWidgetsLocality (); + } + updateBlocklistLabel (); } diff --git a/qt/PrefsDialog.h b/qt/PrefsDialog.h index 2ec88f302..73f8f3afd 100644 --- a/qt/PrefsDialog.h +++ b/qt/PrefsDialog.h @@ -38,6 +38,7 @@ class PrefsDialog: public BaseDialog bool updateWidgetValue (QWidget * widget, int prefKey); void linkWidgetToPref (QWidget * widget, int prefKey); void updateBlocklistLabel (); + void updateDownloadingWidgetsLocality (); void setPref (int key, const QVariant& v); @@ -75,8 +76,10 @@ class PrefsDialog: public BaseDialog Ui::PrefsDialog ui; - key2widget_t myWidgets; const bool myIsServer; + bool myIsLocal; + + key2widget_t myWidgets; QWidgetList myWebWidgets; QWidgetList myWebAuthWidgets; QWidgetList myWebWhitelistWidgets; diff --git a/qt/Session.cc b/qt/Session.cc index 7dbf28bad..381f5a42c 100644 --- a/qt/Session.cc +++ b/qt/Session.cc @@ -22,6 +22,7 @@ #include #include +#include #include // tr_free #include @@ -254,7 +255,8 @@ Session::Session (const QString& configDir, Prefs& prefs): myConfigDir (configDir), myPrefs (prefs), myBlocklistSize (-1), - mySession (0) + mySession (0), + myIsDefinitelyLocalSession (true) { myStats.ratio = TR_RATIO_NA; myStats.uploadedBytes = 0; @@ -346,6 +348,8 @@ Session::isServer () const bool Session::isLocal () const { + if (!mySessionId.isEmpty ()) + return myIsDefinitelyLocalSession; return myRpc.isLocal (); } @@ -794,6 +798,20 @@ Session::updateInfo (tr_variant * d) if (tr_variantDictFindStr (d, TR_KEY_version, &str, NULL) && (mySessionVersion != QString::fromUtf8 (str))) mySessionVersion = QString::fromUtf8 (str); + if (tr_variantDictFindStr (d, TR_KEY_session_id, &str, NULL)) + { + const QString sessionId = QString::fromUtf8 (str); + if (mySessionId != sessionId) + { + mySessionId = sessionId; + myIsDefinitelyLocalSession = rand() % 2; // tr_session_id_is_local (str); + } + } + else + { + mySessionId.clear (); + } + //std::cerr << "Session::updateInfo end" << std::endl; connect (&myPrefs, SIGNAL (changed (int)), this, SLOT (updatePref (int))); diff --git a/qt/Session.h b/qt/Session.h index 2e32e6924..b84b9bdf1 100644 --- a/qt/Session.h +++ b/qt/Session.h @@ -128,6 +128,8 @@ class Session: public QObject tr_session_stats myStats; tr_session_stats myCumulativeStats; QString mySessionVersion; + QString mySessionId; + bool myIsDefinitelyLocalSession; RpcClient myRpc; }; -- 2.40.0