File indexing completed on 2023-10-01 04:14:10

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