File indexing completed on 2024-12-01 06:51:47
0001 // 0002 // C++ Interface: KMuddyMapper 0003 // 0004 // Description: Plugin interface file for the KMuddy mapper. 0005 // 0006 // 0007 // Author: Tomas Mecir, (C) 2007 0008 // 0009 // Copyright: See COPYING file that comes with this distribution 0010 // 0011 0012 #include "kmuddy_mapper.h" 0013 0014 #include "cactionmanager.h" 0015 #include "cmenumanager.h" 0016 #include "cprofilemanager.h" 0017 0018 #include "cmapmanager.h" 0019 #include "cmapview.h" 0020 #include "cmapfilter.h" 0021 #include "cmapzonemanager.h" 0022 0023 #include <kactioncollection.h> 0024 #include <KLocalizedString> 0025 #include <kpluginfactory.h> 0026 #include <kpluginloader.h> 0027 #include <QDockWidget> 0028 #include <map> 0029 0030 K_PLUGIN_CLASS_WITH_JSON(KMuddyMapper, "mapperplugin.json") 0031 0032 struct MapperSession { 0033 CMapManager *manager; 0034 CMapFilter *filter; 0035 }; 0036 0037 struct KMuddyMapperPrivate { 0038 KToggleAction *showmapper; 0039 QDockWidget *docker; 0040 std::map<int, MapperSession *> sessions; 0041 int currentSession; 0042 0043 CMapManager *curManager() { return sessions.count(currentSession) ? sessions[currentSession]->manager : nullptr; }; 0044 CMapZoneManager *curZoneManager() { return sessions.count(currentSession) ? sessions[currentSession]->manager->zoneManager() : nullptr; }; 0045 CMapFilter *curFilter() { return sessions.count(currentSession) ? sessions[currentSession]->filter : nullptr; }; 0046 }; 0047 0048 KMuddyMapper::KMuddyMapper (QObject *, const QVariantList &) 0049 { 0050 d = new KMuddyMapperPrivate; 0051 0052 _priority = 200; 0053 0054 // we need to create an instance of CMapManager, which is the main class 0055 // The class is stored inside a docker, for now - we may want to get rid of all the xmlgui there 0056 KMainWindow *mainWindow = cActionManager::self()->mainWindow (); 0057 d->docker = new QDockWidget (mainWindow); 0058 0059 d->docker->hide (); 0060 d->docker->setWindowTitle (i18n ("Mapper")); 0061 d->docker->setObjectName ("mapper"); 0062 mainWindow->addDockWidget (Qt::RightDockWidgetArea, d->docker); 0063 d->docker->setFloating (true); 0064 connect (d->docker, SIGNAL (visibilityChanged(bool)), this, SLOT (mapperClosed())); 0065 d->docker->setWidget (nullptr); 0066 0067 d->currentSession = 0; 0068 0069 KActionCollection *acol = cActionManager::self()->getACol (); 0070 d->showmapper = new KToggleAction (this); 0071 d->showmapper->setText (i18n ("Show &mapper")); 0072 connect (d->showmapper, SIGNAL (triggered (bool)), this, SLOT (showMapper (bool))); 0073 d->showmapper->setChecked (false); 0074 acol->addAction ("ShowMapper", d->showmapper); 0075 0076 // plug things into the menu 0077 cMenuManager *menu = cMenuManager::self(); 0078 menu->plug (d->showmapper, "view-global"); 0079 } 0080 0081 KMuddyMapper::~KMuddyMapper() 0082 { 0083 // delete the CMapManager, remove menus, and all that 0084 cMenuManager *menu = cMenuManager::self(); 0085 menu->unplug (d->showmapper); 0086 delete d->showmapper; 0087 std::map<int, MapperSession *>::iterator it; 0088 for (it = d->sessions.begin(); it != d->sessions.end(); ++it) { 0089 delete it->second->filter; 0090 delete it->second->manager; 0091 delete it->second; 0092 } 0093 delete d; 0094 } 0095 0096 /** Called when a session has been added. If the session has existed before, fresh is set 0097 to false. This can happen if the plug-in is loaded manually. */ 0098 void KMuddyMapper::sessionAdd (int sess, bool) { 0099 if (d->sessions.count(sess)) return; 0100 MapperSession *mp = new MapperSession; 0101 mp->manager = new CMapManager (d->docker, this, sess); 0102 mp->filter = new CMapFilter (mp->manager); 0103 d->sessions[sess] = mp; 0104 } 0105 0106 /** Called when a session should be removed. Closed is false, if the session isn't being 0107 closed. This hapens when the plug-in is being unloaded manually. */ 0108 void KMuddyMapper::sessionRemove (int sess, bool) { 0109 if (!d->sessions.count(sess)) return; 0110 0111 // TODO: do things if needed, like closing maps 0112 0113 // delete the filter and manager 0114 MapperSession *mp = d->sessions[sess]; 0115 delete mp->filter; 0116 delete mp->manager; 0117 delete mp; 0118 d->sessions.erase(sess); 0119 0120 if (sess == d->currentSession) d->currentSession = 0; 0121 } 0122 0123 /** called when the user switches to another session. Sess is the number of the new session. */ 0124 void KMuddyMapper::sessionSwitch (int sess) { 0125 if (!d->sessions.count(sess)) sessionAdd(sess, false); 0126 CMapManager *manager = d->curManager(); 0127 if (manager && manager->getActiveView()) manager->getActiveView()->hide(); 0128 if (!cProfileManager::self()->settings (sess)) { // this means that it's a profile connection 0129 d->currentSession = 0; 0130 return; 0131 } 0132 0133 d->currentSession = sess; 0134 manager = d->curManager(); 0135 if (manager) { 0136 manager->getActiveView()->show(); 0137 d->docker->setWidget (manager->getActiveView()); 0138 } 0139 } 0140 0141 /** The session has just been connected. Not called when manually loading the plug-in. */ 0142 void KMuddyMapper::connected (int sess) { 0143 if (cActionManager::self()->activeSession() != sess) return; 0144 // Whatever data was loaded previously is now invalid, we need to do that anew. 0145 sessionRemove(sess); 0146 sessionSwitch(sess); 0147 d->sessions[sess]->manager->zoneManager()->loadMapList(); 0148 } 0149 0150 /** The session has just been disconnected. Not called when manually unloading the plug-in. */ 0151 void KMuddyMapper::disconnected (int sess) { 0152 // TODO: deactivate the map perhaps ? 0153 if (sess != d->currentSession) return; 0154 CMapManager *manager = d->curManager(); 0155 if (manager) manager->getActiveView()->hide(); 0156 d->docker->setWidget (nullptr); 0157 } 0158 0159 /** Request to load data. */ 0160 void KMuddyMapper::load (int sess) { 0161 if (!d->sessions.count(sess)) return; 0162 d->sessions[sess]->manager->zoneManager()->loadMapList(); 0163 // TODO: load the current map? First map? Some map? 0164 } 0165 0166 /** Request to save data. */ 0167 void KMuddyMapper::save (int sess) { 0168 if (!d->sessions.count(sess)) return; 0169 CMapZoneManager *zones = d->sessions[sess]->manager->zoneManager(); 0170 zones->saveMapList(); 0171 zones->save(); 0172 } 0173 0174 void KMuddyMapper::processInput (int sess, int phase, cTextChunk * chunk, bool) { 0175 if (phase != 1) return; // don't do things twice 0176 if (sess != d->currentSession) return; 0177 CMapFilter *filter = d->curFilter(); 0178 if (filter) filter->processServerOutput (chunk->toText()); 0179 } 0180 0181 /** Command that is to be sent to the MUD. Aliases have already been expanded. 0182 Command can be modified if desired. If you set dontSend to true, the command won't be 0183 sent and plug-ins with lower priority won't receive this command either. */ 0184 void KMuddyMapper::processCommand (int sess, QString &command, bool &) { 0185 // TODO: send the command for further processing, and handle the situation when the command is to be blocked 0186 if (sess != d->currentSession) return; 0187 CMapFilter *filter = d->curFilter(); 0188 if (filter) command = filter->processCommand (command); 0189 } 0190 0191 void KMuddyMapper::showMapper (bool b) 0192 { 0193 b ? d->docker->show() : d->docker->hide(); 0194 } 0195 0196 void KMuddyMapper::mapperClosed () 0197 { 0198 d->showmapper->setChecked (d->docker->isVisible()); 0199 } 0200 0201 #include "kmuddy_mapper.moc" 0202 #include "moc_kmuddy_mapper.cpp"