Warning, file /games/kmuddy/kmuddy/csessionmanager.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // 0002 // C++ Implementation: cSessionManager 0003 // 0004 // Description: Session manager. 0005 // 0006 /* 0007 Copyright 2002-2008 Tomas Mecir <kmuddy@kmuddy.com> 0008 0009 This program is free software; you can redistribute it and/or 0010 modify it under the terms of the GNU General Public License as 0011 published by the Free Software Foundation; either version 2 of 0012 the License, or (at your option) any later version. 0013 0014 This program is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 GNU General Public License for more details. 0018 0019 You should have received a copy of the GNU General Public License 0020 along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 0023 #include "csessionmanager.h" 0024 0025 #include "cactionmanager.h" 0026 #include "cconnection.h" 0027 #include "cglobalsettings.h" 0028 #include "cinputline.h" 0029 #include "csession.h" 0030 #include "ctabwidget.h" 0031 #include "ctelnet.h" 0032 #include "kmuddy.h" 0033 0034 #include <kapplication.h> 0035 #include <kdialog.h> 0036 #include <klocale.h> 0037 0038 #include <QIcon> 0039 0040 struct cSessionManagerPrivate { 0041 cTabWidget *widget; 0042 0043 cActionManager *am; 0044 0045 /** icons for the tab bar */ 0046 QIcon iconOk, iconNo, iconFlag; 0047 0048 /** should the tab bar be displayed even if there is only one connection? */ 0049 bool wantTabBar; 0050 }; 0051 0052 cSessionManager *cSessionManager::_self = nullptr; 0053 0054 cSessionManager::cSessionManager () : cActionBase ("session-manager", 0) 0055 { 0056 d = new cSessionManagerPrivate; 0057 d->widget = nullptr; 0058 cActionManager::self()->setActiveSession (0); 0059 d->am = cActionManager::self(); 0060 0061 //icons for tabs 0062 d->iconOk = QIcon::fromTheme ("dialog-ok"); 0063 d->iconNo = QIcon::fromTheme ("dialog-cancel"); 0064 d->iconFlag = QIcon::fromTheme ("flag"); 0065 0066 addGlobalEventHandler ("global-settings-changed", 50, PT_NOTHING); 0067 } 0068 0069 cSessionManager::~cSessionManager () 0070 { 0071 removeGlobalEventHandler ("global-settings-changed"); 0072 delete d; 0073 } 0074 0075 cSessionManager *cSessionManager::self() 0076 { 0077 if (!_self) _self = new cSessionManager; 0078 return _self; 0079 } 0080 0081 void cSessionManager::eventNothingHandler (QString event, int) 0082 { 0083 if (event == "global-settings-changed") { 0084 setAlwaysTabBar (cGlobalSettings::self()->getBool ("always-tab-bar")); 0085 } 0086 } 0087 0088 int cSessionManager::count () 0089 { 0090 return d->am->sessions(); 0091 } 0092 0093 void cSessionManager::setMainWidget (cTabWidget *widget) 0094 { 0095 d->widget = widget; 0096 } 0097 0098 int cSessionManager::activeSession () 0099 { 0100 return cActionManager::self()->activeSession(); 0101 } 0102 0103 int cSessionManager::getSessionByTab (int tab) 0104 { 0105 cSession *sess = (cSession *) d->widget->widget (tab); 0106 if (!sess) return -1; 0107 return sess->sess(); 0108 } 0109 0110 int cSessionManager::getTabBySession (int sess) 0111 { 0112 if (!d->am->sessionExists (sess)) return -1; 0113 cSession *s = dynamic_cast<cSession *>(d->am->object ("session", sess)); 0114 if (!s) return -1; 0115 return d->widget->indexOf (s); 0116 } 0117 0118 int cSessionManager::addSession (bool profile) 0119 { 0120 if (KMuddy::isGoingDown()) 0121 return 0; 0122 0123 //first, one very special case - if we only have one session 0124 //and that one is not connected, we return that session 0125 //and don't add any new one. This session will be used for 0126 //the connection that is being established. 0127 if (count() == 1) 0128 { 0129 int which = d->am->sessionList().front(); 0130 cTelnet *telnet = dynamic_cast<cTelnet *>(d->am->object ("telnet", which)); 0131 if (!telnet->isConnected()) 0132 { 0133 cActionManager::self()->setSessionAttrib (which, "profile", profile?1:0); 0134 cGlobalSettings::self()->notifyChange (); 0135 return which; 0136 } 0137 //else: continue 0138 } 0139 0140 //now for the standard operation 0141 0142 //find a first free session ID for the connection 0143 int which = 1; 0144 while (true) { 0145 if (!d->am->sessionExists (which)) break; 0146 ++which; 0147 } 0148 0149 d->am->registerSession (which); 0150 cActionManager::self()->setSessionAttrib (which, "profile", profile?1:0); 0151 cSession *session = new cSession (which, d->widget); 0152 d->widget->addTab (session, i18n ("No connection")); 0153 setIcon (which, IconNo); 0154 0155 // inform everyone 0156 d->am->invokeEvent ("session-created", 0, which); 0157 d->am->invokeEvent ("created", which); 0158 0159 setSession (which); 0160 //just to be sure... 0161 cActionManager::self()->setActiveSession (which); 0162 0163 (KApplication::kApplication())->processEvents(); 0164 0165 //apply global settings to the newly created session 0166 //this also shows/hides the tabbar if needed 0167 cGlobalSettings::self()->notifyChange (); 0168 0169 return which; 0170 } 0171 0172 bool cSessionManager::removeSession (int which, bool dontClose) 0173 { 0174 if (KMuddy::isGoingDown()) 0175 return false; 0176 0177 //reconnect shortcut 0178 QString reconnectText = KMuddy::self()->reconnectText (); 0179 0180 if (!d->am->sessionExists (which)) 0181 return false; 0182 0183 //now the special case - only one session; we do almost nothing! 0184 if (count() == 1) 0185 { 0186 setSessionName (which, i18n ("No connection")); 0187 //no connection... 0188 setIcon (which, IconNo); 0189 cConnection *connection = dynamic_cast<cConnection *>(d->am->object ("connection", which)); 0190 connection->setConnectionClosed (false); 0191 connection->updateMenus (); 0192 d->am->invokeEvent ("message", which, reconnectText); 0193 if (!d->wantTabBar) 0194 d->widget->hideTabBar (); 0195 return true; 0196 } 0197 0198 if (!dontClose) 0199 { 0200 //inform everyone 0201 d->am->invokeEvent ("session-destroyed", 0, which); 0202 d->am->invokeEvent ("destroyed", which); 0203 //remove widget 0204 d->widget->removeTab (getTabBySession (which)); 0205 cSession *sess = dynamic_cast<cSession *>(d->am->object ("session", which)); 0206 delete sess; 0207 d->am->unregisterSession (which); 0208 0209 //hide the tabbar if needed 0210 if ((!d->wantTabBar) && (count() <= 1)) 0211 d->widget->hideTabBar (); 0212 } 0213 else 0214 { 0215 setSessionName (which, i18n ("Connection closed.")); 0216 setIcon (which, IconNo); 0217 cConnection *connection = dynamic_cast<cConnection *>(d->am->object ("connection", which)); 0218 connection->setConnectionClosed (true); 0219 connection->updateMenus (); 0220 d->am->invokeEvent ("message", which, reconnectText); 0221 } 0222 0223 return true; 0224 } 0225 0226 void cSessionManager::setSession (int which) 0227 { 0228 if (!d->am->sessionExists (which)) 0229 return; 0230 //this also invokes changeSession, which updates active session 0231 d->widget->setCurrentIndex (getTabBySession (which)); 0232 } 0233 0234 void cSessionManager::changeSession (int which) 0235 { 0236 d->am->invokeEvent ("deactivated", which); 0237 d->am->invokeEvent ("session-deactivated", 0, which); 0238 0239 cActionManager::self()->setActiveSession (which); 0240 0241 //disable flashing (if enabled) 0242 cSession *sess = dynamic_cast<cSession *>(d->am->object ("session", which)); 0243 if (sess && sess->flashing()) 0244 { 0245 sess->setFlashing (false); 0246 0247 //if flashing was on, icon could be wrong - fix it 0248 cTelnet *telnet = dynamic_cast<cTelnet *>(d->am->object ("telnet", which)); 0249 setIcon (which, telnet->isConnected () ? IconOk : IconNo); 0250 } 0251 0252 d->am->invokeEvent ("session-activated", 0, which); 0253 d->am->invokeEvent ("activated", which); 0254 0255 //global caption 0256 int tab = getTabBySession (which); 0257 KMuddy::self()->setPlainCaption (KDialog::makeStandardCaption (d->widget->tabText (tab))); 0258 0259 //update menus (active items and such) 0260 int s = activeSession(); 0261 cConnection *connection = dynamic_cast<cConnection *>(d->am->object ("connection", s)); 0262 if (!connection) return; // session not initialised yet 0263 connection->updateMenus (); 0264 0265 // update windows 0266 KMuddy::self()->updateWindows (); 0267 0268 //give focus to the input line 0269 //cInputLine::focus[In/Out]Event also restores selections :) 0270 cInputLine *inputline = dynamic_cast<cInputLine *>(d->am->object ("inputline", s)); 0271 inputline->setFocus (); 0272 } 0273 0274 void cSessionManager::setSessionName (int which, QString name, bool defName) 0275 { 0276 cSession *sess = dynamic_cast<cSession *>(d->am->object ("session", which)); 0277 sess->setName (name, defName); 0278 //update the name - maybe it wasn't changed? 0279 name = sess->name(); 0280 int tab = getTabBySession (which); 0281 d->widget->setTabText (tab, name); 0282 //change icon to Ok (we'll change it later if it's not correct) 0283 setIcon (which, IconOk); 0284 if (which == activeSession ()) 0285 KMuddy::self()->setPlainCaption (KDialog::makeStandardCaption (name)); 0286 } 0287 0288 bool cSessionManager::alwaysTabBar () 0289 { 0290 return d->wantTabBar; 0291 } 0292 0293 void cSessionManager::setAlwaysTabBar (bool value) 0294 { 0295 d->wantTabBar = value; 0296 if (d->widget == nullptr) 0297 return; 0298 bool show = true; 0299 if ((!value) && (count() == 1)) show = false; 0300 show ? d->widget->showTabBar() : d->widget->hideTabBar(); 0301 } 0302 0303 void cSessionManager::setIcon (int sess, ProfileIcon icon) 0304 { 0305 int tab = getTabBySession (sess); 0306 QIcon i; 0307 switch (icon) { 0308 case IconOk: i = d->iconOk; break; 0309 case IconNo: i = d->iconNo; break; 0310 case IconFlag: i = d->iconFlag; break; 0311 } 0312 d->widget->setTabIcon (tab, i); 0313 } 0314 0315 void cSessionManager::setNotifyFlag (int sess) 0316 { 0317 cSession *session = dynamic_cast<cSession *>(d->am->object ("session", sess)); 0318 session->setFlashing (true); 0319 0320 cConnection *connection = dynamic_cast<cConnection *>(d->am->object ("connection", sess)); 0321 bool connClosed = connection->connectionClosed(); 0322 setIcon (sess, connClosed ? IconNo : IconFlag); 0323 } 0324 0325