File indexing completed on 2024-04-14 14:32:10

0001 //
0002 // C++ Implementation: cwindowlist
0003 //
0004 // Description:
0005 //
0006 /*
0007 Copyright 2004-2011 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 "cwindowlist.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "cconsole.h"
0027 #include "cglobalsettings.h"
0028 #include "dialogs/dlgoutputwindow.h"
0029 #include "cprofilemanager.h"
0030 
0031 #include <kconfig.h>
0032 #include <kconfiggroup.h>
0033 
0034 using namespace std;
0035 
0036 cWindowList::cWindowList (int sess)
0037   : cActionBase ("windowlist", sess)
0038 {
0039   file = cProfileManager::self()->profilePath (sess) + "/windowlist";
0040   load ();
0041 
0042   addGlobalEventHandler ("global-settings-changed", 50, PT_NOTHING);
0043 }
0044 
0045 cWindowList::~cWindowList ()
0046 {
0047   save ();
0048 
0049   removeGlobalEventHandler ("global-settings-changed");
0050 
0051   //destroy all windows
0052   map<QString, dlgOutputWindow *>::iterator it;
0053   for (it = windows.begin(); it != windows.end(); ++it)
0054     delete it->second;
0055   windows.clear ();
0056 }
0057 
0058 void cWindowList::eventNothingHandler (QString event, int)
0059 {
0060   if (event == "global-settings-changed") {
0061     cGlobalSettings *gs = cGlobalSettings::self();
0062     adjustFonts (gs->getFont ("console-font"));
0063     applySettings (gs->getBool ("allow-blink"), gs->getInt ("indent"));
0064   }
0065 }
0066 
0067 bool cWindowList::exists (const QString &name)
0068 {
0069   if (windows.count (name))
0070     return true;
0071   return false;
0072 }
0073 
0074 bool cWindowList::add (const QString &name, bool autoadd)
0075 {
0076   if (exists (name)) return false;
0077   //won't allow empty window name
0078   if (name.isEmpty()) return false;
0079   //create the window
0080   dlgOutputWindow *owin = new dlgOutputWindow (cActionManager::self()->mainWidget());
0081   //remember it
0082   windows[name] = owin;
0083   owin->setSession (sess());
0084   owin->setOutputWindowName(name);
0085   
0086   //let the window be shown by default, unless added by autorestore
0087   if(!autoadd)
0088     owin->show ();
0089   
0090   // set settings
0091   cGlobalSettings::self()->notifyChange();
0092 
0093   return true;
0094 }
0095 
0096 bool cWindowList::remove (const QString &name)
0097 {
0098   if (!exists (name)) return false;
0099 
0100   //get the window
0101   dlgOutputWindow *owin = windows[name];
0102   //hide it
0103   owin->hide ();
0104   //delete it
0105   delete owin;
0106   //remove it from our mapping
0107   windows.erase (name);
0108   //add it to the list of windows, that are to be erased from config-file
0109   toerase.push_back (name);
0110   return true;
0111 }
0112 
0113 QStringList cWindowList::windowList ()
0114 {
0115   QStringList lst;
0116   map<QString, dlgOutputWindow *>::iterator it;
0117   for (it = windows.begin(); it != windows.end(); ++it)
0118     lst.push_back (it->first);
0119   return lst;
0120 }
0121 
0122 bool cWindowList::show (const QString &name)
0123 {
0124   if (!exists (name)) return false;
0125 
0126   dlgOutputWindow *owin = windows[name];
0127 
0128   owin->show ();
0129   return true;
0130 }
0131 
0132 bool cWindowList::hide (const QString &name)
0133 {
0134   if (!exists (name)) return false;
0135 
0136   dlgOutputWindow *owin = windows[name];
0137 
0138   owin->hide ();
0139   return true;
0140 }
0141 
0142 void cWindowList::toggle (const QString &name)
0143 {
0144   if (!exists (name)) return;
0145   if (windows[name]->isVisible())
0146     windows[name]->hide ();
0147   else
0148     windows[name]->show ();
0149 }
0150 
0151 bool cWindowList::isShown (const QString &name)
0152 {
0153   if (!exists (name)) return false;
0154   return windows[name]->isVisible();
0155 }
0156 
0157 void cWindowList::textToWindow (const QString &name, cTextChunk *chunk)
0158 {
0159   if (!exists (name)) return;
0160 
0161   //add text to the window
0162   windows[name]->addLine(chunk);
0163   // switch-window is used so that others can determine which window was used
0164   invokeEvent ("switch-window", sess(), name);
0165   invokeEvent ("displayed-line", sess(), chunk);
0166 }
0167 
0168 void cWindowList::load ()
0169 {
0170   QStringList groups;
0171   QPoint pos;
0172   bool vis;
0173 
0174   KConfig *config = new KConfig (file);
0175 
0176   groups = config->groupList();
0177 
0178   if(groups.empty() == true)
0179     return;
0180 
0181   //traverse through list of groups, loading each window
0182   for ( QStringList::Iterator it = groups.begin(); it != groups.end(); ++it )
0183     {
0184       name = *it;
0185       if(name.indexOf("KMuddywin_", 0) != -1)
0186       {
0187         KConfigGroup g = config->group (name);
0188         name.replace("KMuddywin_", QString());
0189         if(!add(name, true))
0190           return;
0191         windows[name]->setMinimumSize(g.readEntry("Size", QSize()));
0192         pos = g.readEntry("Position", QPoint());
0193         windows[name]->move(pos);
0194         vis = g.readEntry("Visible", true);
0195         if(vis)
0196           show(name);
0197       }
0198     }
0199   delete config;
0200 }
0201 
0202 void cWindowList::save ()
0203 {
0204   KConfig *config = new KConfig (file);
0205 
0206   //erase removed windows from the configfile
0207   if(!toerase.empty())
0208   {
0209     for(QStringList::Iterator it = toerase.begin(); it != toerase.end(); ++it)
0210     {
0211       config->deleteGroup("KMuddywin_"+*it);
0212     }
0213   }
0214 
0215   //save all windows
0216   map<QString, dlgOutputWindow *>::iterator it;
0217   for (it = windows.begin(); it != windows.end(); ++it)
0218   {
0219     KConfigGroup g = config->group ("KMuddywin_"+it->first);
0220     g.writeEntry("Size", it->second->size());
0221     g.writeEntry("Position", it->second->pos());
0222     g.writeEntry("Visible", it->second->isVisible());
0223   }
0224 
0225   delete config;
0226 }
0227 
0228 void cWindowList::adjustFonts(QFont font)
0229 {
0230   map<QString, dlgOutputWindow *>::iterator it;
0231   for (it = windows.begin(); it != windows.end(); ++it)
0232   {
0233     it->second->setFont(font);
0234   }
0235 }
0236 
0237 void cWindowList::applySettings (bool allowblinking, int indentvalue)
0238 {
0239   map<QString, dlgOutputWindow *>::iterator it;
0240   for (it = windows.begin(); it != windows.end(); ++it)
0241   {
0242     cConsole *console = it->second->console();
0243     console->setEnableBlinking (allowblinking);
0244     console->setIndentation (indentvalue);
0245   }
0246 }
0247 
0248