From 8f5fe8bae2be0e940a68dccaf97fbc7c9db112ad Mon Sep 17 00:00:00 2001 From: thunder2 Date: Sat, 17 Jul 2010 23:19:43 +0000 Subject: [PATCH] - added new status menu to tray menu - moved internal status functions from MessengerWindow to MainWindow - cleaned some includes git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3300 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- retroshare-gui/src/gui/MainWindow.cpp | 207 ++++++++++++++++++++- retroshare-gui/src/gui/MainWindow.h | 29 ++- retroshare-gui/src/gui/MessengerWindow.cpp | 134 +++---------- retroshare-gui/src/gui/MessengerWindow.h | 8 +- retroshare-gui/src/gui/MessengerWindow.ui | 27 --- retroshare-gui/src/main.cpp | 5 +- 6 files changed, 252 insertions(+), 158 deletions(-) diff --git a/retroshare-gui/src/gui/MainWindow.cpp b/retroshare-gui/src/gui/MainWindow.cpp index 50a82703f..2df19506d 100644 --- a/retroshare-gui/src/gui/MainWindow.cpp +++ b/retroshare-gui/src/gui/MainWindow.cpp @@ -19,9 +19,7 @@ * Boston, MA 02110-1301, USA. ****************************************************************/ -#include #include -#include #include #include #include @@ -51,10 +49,12 @@ #include "QuickStartWizard.h" #include "gui/TurtleRouterDialog.h" +#include "idle/idle.h" #include "statusbar/peerstatus.h" #include "statusbar/natstatus.h" #include "statusbar/ratesstatus.h" +#include "rsiface/rsstatus.h" #include "rsiface/rsiface.h" #include "rsiface/rspeers.h" @@ -115,19 +115,30 @@ /*static*/ MainWindow *MainWindow::Create () { if (_instance == NULL) { - _instance = new MainWindow (); + /* _instance is set in constructor */ + new MainWindow (); } return _instance; } +/*static*/ MainWindow *MainWindow::getInstance() +{ + return _instance; +} + /** Constructor */ MainWindow::MainWindow(QWidget* parent, Qt::WFlags flags) - : RWindow("MainWindow", parent, flags) + : RWindow("MainWindow", parent, flags), maxTimeBeforeIdle(30) { /* Invoke the Qt Designer generated QObject setup routine */ ui.setupUi(this); - + + _instance = this; + + m_bStatusLoadDone = false; + isIdle = false; + if (Settings->value(QString::fromUtf8("FirstRun"), true).toBool()) { Settings->setValue(QString::fromUtf8("FirstRun"), false); @@ -273,8 +284,11 @@ MainWindow::MainWindow(QWidget* parent, Qt::WFlags flags) /* Creates a tray icon with a context menu and adds it to the system's * notification area. */ createTrayIcon(); + loadOwnStatus(); // hack; placed in constructor to preempt sendstatus, so status loaded from file + idle = new Idle(); idle->start(); + connect(idle, SIGNAL(secondsIdle(int)), this, SLOT(checkAndSetIdle(int))); QTimer *timer = new QTimer(this); timer->connect(timer, SIGNAL(timeout()), this, SLOT(updateStatus())); @@ -327,6 +341,11 @@ void MainWindow::createTrayIcon() QObject::connect(trayMenu, SIGNAL(aboutToShow()), this, SLOT(updateMenu())); toggleVisibilityAction = trayMenu->addAction(QIcon(IMAGE_RETROSHARE), tr("Show/Hide"), this, SLOT(toggleVisibilitycontextmenu())); + + QMenu *pStatusMenu = trayMenu->addMenu(tr("Status")); + initializeStatusObject(pStatusMenu); + connect(pStatusMenu, SIGNAL(triggered (QAction*)), this, SLOT(statusChanged(QAction*))); + trayMenu->addSeparator(); trayMenu->addAction(_messengerwindowAct); trayMenu->addAction(_messagesAct); @@ -802,4 +821,182 @@ void MainWindow::setStyle() } +/* get own status */ +static int getOwnStatus() +{ + std::string ownId = rsPeers->getOwnId(); + StatusInfo si; + std::list statusList; + std::list::iterator it; + + if (!rsStatus->getStatus(statusList)) { + return -1; + } + + for (it = statusList.begin(); it != statusList.end(); it++){ + if (it->id == ownId) + return it->status; + } + + return -1; +} + +/* set status object to status value */ +static void setStatusObject(QObject *pObject, int nStatus) +{ + QMenu *pMenu = dynamic_cast(pObject); + if (pMenu) { + /* set action in menu */ + foreach(QObject *pObject, pMenu->children()) { + QAction *pAction = qobject_cast (pObject); + if (pAction == NULL) { + continue; + } + + if (pAction->data().toInt() == nStatus) { + pAction->setChecked(true); + break; + } + } + return; + } + QComboBox *pComboBox = dynamic_cast(pObject); + if (pComboBox) { + /* set index of combobox */ + int nIndex = pComboBox->findData(nStatus, Qt::UserRole); + if (nIndex != -1) { + pComboBox->setCurrentIndex(nIndex); + } + return; + } + + /* add more objects here */ +} + +/** Load own status Online,Away,Busy **/ +void MainWindow::loadOwnStatus() +{ + m_bStatusLoadDone = true; + + int nStatus = getOwnStatus(); + + for (std::set ::iterator it = m_apStatusObjects.begin(); it != m_apStatusObjects.end(); it++) { + setStatusObject(*it, nStatus); + } +} + +void MainWindow::checkAndSetIdle(int idleTime) +{ + if ((idleTime >= (int) maxTimeBeforeIdle) && !isIdle) { + setIdle(true); + }else + if((idleTime < (int) maxTimeBeforeIdle) && isIdle) { + setIdle(false); + } + + return; +} + +void MainWindow::setIdle(bool idle) +{ + isIdle = idle; + setStatus(NULL, getOwnStatus()); +} + +/* add and initialize status object */ +void MainWindow::initializeStatusObject(QObject *pObject) +{ + if (m_apStatusObjects.find(pObject) != m_apStatusObjects.end()) { + return; + } + + m_apStatusObjects.insert(m_apStatusObjects.end(), pObject); + + QMenu *pMenu = dynamic_cast(pObject); + if (pMenu) { + /* initialize menu */ + QActionGroup *pGroup = new QActionGroup(pMenu); + + QAction *pAction = new QAction(QIcon(":/images/im-user.png"), tr("Online"), pMenu); + pAction->setData(RS_STATUS_ONLINE); + pAction->setCheckable(true); + pMenu->addAction(pAction); + pGroup->addAction(pAction); + + pAction = new QAction(QIcon(":/images/im-user-busy.png"), tr("Busy"), pMenu); + pAction->setData(RS_STATUS_BUSY); + pAction->setCheckable(true); + pMenu->addAction(pAction); + pGroup->addAction(pAction); + + pAction = new QAction(QIcon(":/images/im-user-away.png"), tr("Away"), pMenu); + pAction->setData(RS_STATUS_AWAY); + pAction->setCheckable(true); + pMenu->addAction(pAction); + pGroup->addAction(pAction); + } else { + /* initialize combobox */ + QComboBox *pComboBox = dynamic_cast(pObject); + if (pComboBox) { + pComboBox->addItem(QIcon(":/images/im-user.png"), tr("Online"), RS_STATUS_ONLINE); + pComboBox->addItem(QIcon(":/images/im-user-busy.png"), tr("Busy"), RS_STATUS_BUSY); + pComboBox->addItem(QIcon(":/images/im-user-away.png"), tr("Away"), RS_STATUS_AWAY); + } + /* add more objects here */ + } + + if (m_bStatusLoadDone) { + /* loadOwnStatus done, set own status directly */ + int nStatus = getOwnStatus(); + if (nStatus != -1) { + setStatusObject(pObject, nStatus); + } + } +} + +/* remove status object */ +void MainWindow::removeStatusObject(QObject *pObject) +{ + m_apStatusObjects.erase(pObject); +} + +/** Save own status Online,Away,Busy **/ +void MainWindow::setStatus(QObject *pObject, int nStatus) +{ + RsPeerDetails detail; + std::string ownId = rsPeers->getOwnId(); + + if (!rsPeers->getPeerDetails(ownId, detail)) { + return; + } + + StatusInfo si; + + if (isIdle && nStatus == (int) RS_STATUS_ONLINE) { + /* set idle state only when I am in online state */ + nStatus = RS_STATUS_INACTIVE; + } + + si.id = ownId; + si.status = nStatus; + + rsStatus->sendStatus(si); + + /* set status in all status objects, but the calling one */ + for (std::set ::iterator it = m_apStatusObjects.begin(); it != m_apStatusObjects.end(); it++) { + if (*it != pObject) { + setStatusObject(*it, nStatus); + } + } +} + +/* new status from context menu */ +void MainWindow::statusChanged(QAction *pAction) +{ + if (pAction == NULL) { + return; + } + + setStatus(pAction->parent(), pAction->data().toInt()); +} diff --git a/retroshare-gui/src/gui/MainWindow.h b/retroshare-gui/src/gui/MainWindow.h index 5a94d1b88..98f054b79 100644 --- a/retroshare-gui/src/gui/MainWindow.h +++ b/retroshare-gui/src/gui/MainWindow.h @@ -22,10 +22,8 @@ #ifndef _MainWindow_H #define _MainWindow_H -#include -#include -#include #include +#include #ifdef UNFINISHED #include "unfinished/ApplicationWindow.h" @@ -38,8 +36,8 @@ #include "ui_MainWindow.h" #include "gui/common/rwindow.h" -#include "idle/idle.h" +class Idle; class PeerStatus; class NATStatus; class RatesStatus; @@ -88,6 +86,7 @@ public: /** Create main window */ static MainWindow *Create (); + static MainWindow *getInstance(); /** Destructor. */ ~MainWindow(); @@ -133,11 +132,17 @@ public: static void installGroupChatNotifier(); + /* initialize widget with status informations, status constant stored in data or in Qt::UserRole */ + void initializeStatusObject(QObject *pObject); + void removeStatusObject(QObject *pObject); + void setStatus(QObject *pObject, int nStatus); + public slots: void updateHashingInfo(const QString&) ; void displayErrorMessage(int,int,const QString&) ; void postModDirectories(bool update_local); void displayDiskSpaceWarning(int loc,int size_limit_mb) ; + void checkAndSetIdle(int idleTime); protected: /** Default Constructor */ @@ -177,6 +182,7 @@ private slots: void showMess(); void showSettings(); void setStyle(); + void statusChanged(QAction *pAction); /** Called when user attempts to quit via quit button*/ void doQuit(); @@ -223,8 +229,19 @@ private: RatesStatus *ratesstatus; QLabel *_hashing_info_label ; - - QAction *messageAction ; + + QAction *messageAction ; + + /* Status */ + std::set m_apStatusObjects; // added objects for status + bool m_bStatusLoadDone; + + void loadOwnStatus(); + + // idle function + void setIdle(bool Idle); + bool isIdle; + const unsigned long maxTimeBeforeIdle; /** Qt Designer generated object */ Ui::MainWindow ui; diff --git a/retroshare-gui/src/gui/MessengerWindow.cpp b/retroshare-gui/src/gui/MessengerWindow.cpp index 1dcc01aa3..be9e5459d 100644 --- a/retroshare-gui/src/gui/MessengerWindow.cpp +++ b/retroshare-gui/src/gui/MessengerWindow.cpp @@ -31,6 +31,7 @@ #include "rshare.h" #include "MessengerWindow.h" +#include "MainWindow.h" #include "RsAutoUpdatePage.h" #include "chat/PopupChatDialog.h" @@ -131,7 +132,7 @@ void MessengerWindow::releaseInstance() /** Constructor */ MessengerWindow::MessengerWindow(QWidget* parent, Qt::WFlags flags) - : RWindow("MessengerWindow", parent, flags), maxTimeBeforeIdle(30) + : RWindow("MessengerWindow", parent, flags) { /* Invoke the Qt Designer generated object setup routine */ ui.setupUi(this); @@ -147,6 +148,7 @@ MessengerWindow::MessengerWindow(QWidget* parent, Qt::WFlags flags) connect(ui.clearButton, SIGNAL(clicked()), this, SLOT(clearFilter())); connect(ui.messagelineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(savestatusmessage())); + connect(ui.statuscomboBox, SIGNAL(activated(int)), this, SLOT(statusChanged(int))); connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(filterRegExpChanged())); QTimer *timer = new QTimer(this); @@ -187,8 +189,10 @@ MessengerWindow::MessengerWindow(QWidget* parent, Qt::WFlags flags) // load settings processSettings(true); - isIdle = false; - loadOwnStatus(); // hack; placed in constructor to preempt sendstatus, so status loaded from file + MainWindow *pMainWindow = MainWindow::getInstance(); + if (pMainWindow) { + pMainWindow->initializeStatusObject(ui.statuscomboBox); + } insertPeers(); updateAvatar(); loadmystatusmessage(); @@ -206,6 +210,11 @@ MessengerWindow::~MessengerWindow () { // save settings processSettings(false); + + MainWindow *pMainWindow = MainWindow::getInstance(); + if (pMainWindow) { + pMainWindow->removeStatusObject(ui.statuscomboBox); + } } void MessengerWindow::processSettings(bool bLoad) @@ -367,7 +376,6 @@ void MessengerWindow::updateMessengerDisplay() } insertPeers(); - savestatus(); } /* get the list of peers from the RsIface. */ @@ -973,13 +981,25 @@ void MessengerWindow::loadmystatusmessage() } /** Save own status message */ -void MessengerWindow::savestatusmessage() +void MessengerWindow::savestatusmessage() { Settings->setValueToGroup("Profile", "StatusMessage",ui.messagelineEdit->text()); rsMsgs->setCustomStateString(ui.messagelineEdit->text().toStdString()); } +void MessengerWindow::statusChanged(int index) +{ + if (index < 0) { + return; + } + + MainWindow *pMainWindow = MainWindow::getInstance(); + if (pMainWindow) { + pMainWindow->setStatus(ui.statuscomboBox, ui.statuscomboBox->itemData(index, Qt::UserRole).toInt()); + } +} + void MessengerWindow::on_actionSort_Peers_Descending_Order_activated() { ui.messengertreeWidget->sortItems ( COLUMN_NAME, Qt::DescendingOrder ); @@ -1007,110 +1027,6 @@ void MessengerWindow::displayMenu() ui.displaypushButton->setMenu(lookmenu); } -/** Load own status Online,Away,Busy **/ -void MessengerWindow::loadOwnStatus() -{ - - std::string ownId = rsPeers->getOwnId(); - - StatusInfo si; - std::list statusList; - std::list::iterator it; - - if (!rsStatus->getStatus(statusList)) - { - return; - } - - for(it=statusList.begin(); it != statusList.end(); it++){ - if(it->id == ownId) - si = *it; - } - - - /* set status mode */ - int statusIndex = 0; - switch(si.status) - { - case RS_STATUS_AWAY: - statusIndex = 2; - break; - case RS_STATUS_BUSY: - statusIndex = 1; - break; - default: - case RS_STATUS_ONLINE: - statusIndex = 0; - break; - } - - ui.statuscomboBox->setCurrentIndex(statusIndex); - -} - -/** Save own status Online,Away,Busy **/ -void MessengerWindow::savestatus() -{ - - RsPeerDetails detail; - std::string ownId = rsPeers->getOwnId(); - - if (!rsPeers->getPeerDetails(ownId, detail)) - { - return; - } - - StatusInfo si; - int statusIndex; - - if(!isIdle) - statusIndex = ui.statuscomboBox->currentIndex(); - else - statusIndex = 3; - - /* Check if status has changed */ - int status = 0; - switch(statusIndex) - { - case 3: - status = RS_STATUS_INACTIVE; - break; - case 2: - status = RS_STATUS_AWAY; - break; - case 1: - status = RS_STATUS_BUSY; - break; - default: - case 0: - status = RS_STATUS_ONLINE; - break; - } - - si.id = ownId; - si.status = status; - - rsStatus->sendStatus(si); - - return; -} - -void MessengerWindow::checkAndSetIdle(int idleTime){ - - if((idleTime >= (int) maxTimeBeforeIdle) && !isIdle){ - setIdle(true); - }else - if((idleTime < (int) maxTimeBeforeIdle) && isIdle){ - setIdle(false); - } - - return; -} - -void MessengerWindow::setIdle(bool idle){ - isIdle = idle; -} - /* clear Filter */ void MessengerWindow::clearFilter() { diff --git a/retroshare-gui/src/gui/MessengerWindow.h b/retroshare-gui/src/gui/MessengerWindow.h index 8abf89f33..56a33f5b5 100644 --- a/retroshare-gui/src/gui/MessengerWindow.h +++ b/retroshare-gui/src/gui/MessengerWindow.h @@ -47,8 +47,6 @@ public slots: void updatePeersAvatar(const QString& peer_id); void updateAvatar(); void loadmystatusmessage(); - void loadOwnStatus(); - void checkAndSetIdle(int idleTime); LogoBar & getLogoBar() const; @@ -91,6 +89,7 @@ private slots: void changeAvatarClicked(); + void statusChanged(int index); void savestatusmessage(); void on_actionSort_Peers_Descending_Order_activated(); @@ -115,12 +114,7 @@ private: /* (2) Utility Fns */ QTreeWidgetItem *getCurrentPeer(); - void savestatus(); void insertPeers(); - // idle function - void setIdle(bool Idle); - bool isIdle; - const unsigned long maxTimeBeforeIdle; void FilterItems(); bool FilterItem(QTreeWidgetItem *pItem, QString &sPattern); diff --git a/retroshare-gui/src/gui/MessengerWindow.ui b/retroshare-gui/src/gui/MessengerWindow.ui index 157d91b6a..c3a214eab 100644 --- a/retroshare-gui/src/gui/MessengerWindow.ui +++ b/retroshare-gui/src/gui/MessengerWindow.ui @@ -125,33 +125,6 @@ p, li { white-space: pre-wrap; } 0 - - - Online - - - - :/images/im-user.png:/images/im-user.png - - - - - Busy - - - - :/images/im-user-busy.png:/images/im-user-busy.png - - - - - Away - - - - :/images/im-user-away.png:/images/im-user-away.png - - diff --git a/retroshare-gui/src/main.cpp b/retroshare-gui/src/main.cpp index 991b00f55..f2dc0abcd 100644 --- a/retroshare-gui/src/main.cpp +++ b/retroshare-gui/src/main.cpp @@ -19,7 +19,6 @@ * Boston, MA 02110-1301, USA. ****************************************************************/ -#include #include #include #include "gui/MainWindow.h" @@ -34,6 +33,7 @@ #include "gui/GenCertDialog.h" #include "gui/settings/rsharesettings.h" #include "gui/connect/ConfCertDialog.h" +#include "idle/idle.h" /*** WINDOWS DON'T LIKE THIS - REDEFINES VER numbers. #include @@ -194,12 +194,9 @@ int main(int argc, char *argv[]) w->installGroupChatNotifier(); - QObject::connect(w->idle, SIGNAL(secondsIdle(int)), w->messengerWindow, SLOT(checkAndSetIdle(int))); - /* only show window, if not startMinimized */ if(!Settings->value(QString::fromUtf8("StartMinimized"), false).toBool()) { - w->show(); }