summaryrefslogtreecommitdiffstats
path: root/ksim/ksimview.cpp
blob: 572579c7ab53533f53015663781f5fe182668b48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*  ksim - a system monitor for kde
 *
 *  Copyright (C) 2001  Robbie Ward <linuxphreak@gmx.co.uk>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "ksimview.h"
#include "ksimview.moc"

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ksimsysinfo.h"
#include "ksim.h"
#include "ksimframe.h"
#include <label.h>
#include <common.h>
#include <themeloader.h>
#include <pluginmodule.h>
#include <pluginloader.h>
#include <ksimconfig.h>
#include "ksimpref.h"
#include "baselist.h"
#include <themetypes.h>

#include <kapplication.h>
#include <klocale.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>
#include <kdesktopfile.h>
#include <twin.h>
#include <krun.h>

#include <tqfile.h>
#include <tqbitmap.h>
#include <tqtimer.h>
#include <tqlayout.h>
#include <tqobjectlist.h>
#include <tqpainter.h>
#include <tqcursor.h>
#include <tqpopupmenu.h>
#include <tqvbox.h>

#include <unistd.h>
#include <sys/param.h>

#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif

KSim::MainView::MainView(TDEConfig *config,
   bool loadPlugins, KSim::PanelExtension *topLevel,
   const char *name) : DCOPObject("KSim"),
   TQWidget(topLevel, name)
{
  // create the local "themes" and "monitors" dirs
  makeDirs();

  setBackgroundMode(PaletteBackground);

  m_topLevel = topLevel;
  m_oldLocation = 1;
  m_prefDialog = 0L;

  m_config = new KSim::Config(config);

  // Make sure the current theme is valid
  // if not then revert to the default theme
  KSim::ThemeLoader::self().validate();

  // parse the current theme in case it doesn't have the correct dir structure
  if (KSim::ThemeLoader::currentName() != "ksim")
    KSim::ThemeLoader::self().parseDir(KSim::ThemeLoader::currentUrl(),
       KSim::ThemeLoader::currentAlternative());

  kdDebug(2003) << "loadPlugins: " << loadPlugins << endl;

  m_subLayout = new TQVBoxLayout(this);

  m_topFrame = new KSim::Frame(KSim::Types::TopFrame, this);
  m_subLayout->addWidget(m_topFrame);

  m_sizeLayout = new TQHBoxLayout;
  m_subLayout->addLayout(m_sizeLayout);

  m_leftFrame = new KSim::Frame(KSim::Types::LeftFrame, this);
  m_sizeLayout->addWidget(m_leftFrame);

  m_pluginLayout = new TQBoxLayout(TQBoxLayout::TopToBottom);
  m_sizeLayout->addLayout(m_pluginLayout);

  TQVBoxLayout *vb = new TQVBoxLayout;
  TQSpacerItem *item = new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Expanding);
  vb->addItem(item);
  
  m_hostLabel = new KSim::Label(KSim::Types::Host, this);
  m_hostLabel->installEventFilter(this);
  vb->addWidget(m_hostLabel);
  
  m_pluginLayout->addLayout(vb);

  char hostName[MAXHOSTNAMELEN];
  if (gethostname(hostName, sizeof(hostName)))
    m_hostLabel->setText(i18n("Unknown"));
  else {
    TQCString host(hostName);
    int dotLocation = host.find(".");
    if (!m_config->displayFqdn() && dotLocation != -1)
      host.truncate(dotLocation);

    m_hostLabel->setText(host);
  }

  vb = new TQVBoxLayout;
  item = new TQSpacerItem(0, 0, TQSizePolicy::Expanding, TQSizePolicy::Expanding);
  vb->addItem(item);
  m_sysinfo = new KSim::Sysinfo(m_config, this);
  vb->addWidget(m_sysinfo);
  m_pluginLayout->addLayout(vb);

  m_bottomFrame = new KSim::Frame(KSim::Types::BottomFrame, this);
  m_subLayout->addWidget(m_bottomFrame);

  m_rightFrame = new KSim::Frame(KSim::Types::RightFrame, this);
  m_sizeLayout->addWidget(m_rightFrame);

  connect(&KSim::PluginLoader::self(),
     TQT_SIGNAL(pluginLoaded(const KSim::Plugin &)),
     this, TQT_SLOT(addMonitor(const KSim::Plugin &)));

  KSim::ThemeLoader::self().themeColours(this);

  // load the plugins and create the plugin menu
  if (loadPlugins) {
    addPlugins();
  }
  
  connect(&m_maskTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotMaskMainView()));
}

KSim::MainView::~MainView()
{
  delete m_config;
}

void KSim::MainView::show()
{
  maskMainView();
  TQWidget::show();
}

void KSim::MainView::cleanup()
{
  // delete our configuration dialog and remove
  // the plugin config pages from the configuration
  // dialog before we unload our plugins
  delete m_prefDialog;

  // Clean up the singleton instances
  KSim::PluginLoader::cleanup();
}

KSim::Config *KSim::MainView::config() const
{
  return m_config;
}

void KSim::MainView::makeDirs()
{
  TQString homeDir = locateLocal("data", "ksim");
  TQString themeDir = homeDir + TQString::fromLatin1("/themes");
  TQString monitorDir = homeDir + TQString::fromLatin1("/monitors");

  // return true if the dirs already exist
  if (TQFile::exists(themeDir) && TQFile::exists(monitorDir))
    return;

  bool themeCreated = KStandardDirs::makeDir(themeDir);
  bool monitorCreated = KStandardDirs::makeDir(monitorDir);

  if (!themeCreated || !monitorCreated)
  {
    KMessageBox::sorry(0, i18n("There was an error while trying to create "
       "the local folders. This could be caused by permission problems."));
  }
}

const TQString &KSim::MainView::hostname() const
{
  return m_hostLabel->text();
}

void KSim::MainView::slotMaskMainView()
{
  maskMainView();
}

void KSim::MainView::maskMainView()
{
  if (!m_topFrame->background()->mask() ||
      !m_leftFrame->background()->mask() ||
      !m_rightFrame->background()->mask() ||
      !m_bottomFrame->background()->mask())
  {
    topLevelWidget()->clearMask();
    return;
  }
  
  TQBitmap topPixmap(*m_topFrame->background()->mask());
  TQBitmap leftPixmap(*m_leftFrame->background()->mask());
  TQBitmap rightPixmap(*m_rightFrame->background()->mask());
  TQBitmap bottomPixmap(*m_bottomFrame->background()->mask());

  TQSize insideSize(m_pluginLayout->geometry().size());

  // make a cleared bigrect where we can put our pixmap masks on
  TQBitmap bigBitmap(topLevelWidget()->size(), true);

  // better return if our bitmap is null so we can avoid crashes
  if (bigBitmap.isNull())
    return;

  TQPoint ofs = mapTo(topLevelWidget(), TQPoint(0,0));
  int ofsX = ofs.x();
  int ofsY = ofs.y();

  TQPainter painter;
  painter.begin(&bigBitmap);
  painter.setBrush(color1);
  painter.setPen(color1);
  TQRect rect = m_pluginLayout->geometry();
  rect.moveBy(ofsX, ofsY);
  painter.drawRect(rect);
  painter.drawPixmap(ofsX, ofsY, topPixmap);
  painter.drawPixmap(ofsX, topPixmap.height()+ofsY, leftPixmap);
  painter.drawPixmap(ofsX+insideSize.width() + leftPixmap.width(),
     ofsY+ topPixmap.height(), rightPixmap);
  painter.drawPixmap(ofsX, ofsY + height() - bottomPixmap.height(),
     bottomPixmap);

  painter.end();
  topLevelWidget()->setMask(bigBitmap);
}

void KSim::MainView::reparseConfig(bool emitReload,
   const KSim::ChangedPluginList &list)
{
  kdDebug(2003) << "creating a new view" << endl;

  // Update the theme information for the theme classes
  bool themeChanged = KSim::ThemeLoader::self().isDifferent();
  if (themeChanged)
    KSim::ThemeLoader::self().reload();

  const KSim::PluginList &pluginList = KSim::PluginLoader::self().pluginList();
  KSim::PluginList::ConstIterator plugin;
  for (plugin = pluginList.begin(); plugin != pluginList.end(); ++plugin) {
    if ((*plugin).configPage()) {
      (*plugin).configPage()->saveConfig();
      TDEConfig *conf = (*plugin).configPage()->config();
      if (conf) conf->sync();
    }

    if (!(*plugin).isDifferent() && (*plugin).isEnabled()) {
      kdDebug(2003) << "Recreating " << (*plugin).name() << "'s view" << endl;
      kapp->processEvents();
      // Make sure the plugins background color is
      // set to the correct theme color
      if (themeChanged && (*plugin).view())
        KSim::ThemeLoader::self().themeColours((*plugin).view());

      // Reload the config file and and recreate the plugins view
      if ((*plugin).configPage() && (*plugin).configPage()->config())
        (*plugin).configPage()->config()->reparseConfiguration();

      if ((*plugin).view()) {
        (*plugin).view()->config()->reparseConfiguration();
        (*plugin).view()->reparseConfig();
      }
    }
  }

  ChangedPluginList::ConstIterator it;
  for (it = list.begin(); it != list.end(); ++it) {
    if ((*it).isDifferent()) {
      if ((*it).isEnabled()) { // Go through the added/removed plugins and load/unload them
        const KDesktopFile kdf((*it).filename());
        addPlugin(kdf);
        m_prefDialog->createPage((*it).libName());
      }
      else {
        m_prefDialog->removePage((*it).libName());
        const KDesktopFile kdf((*it).filename());
        removePlugin(kdf);
      }
    }
  }

  // reparse various non-plugin specific code
  // recreate the plugin menu and the dock icon
  KSim::BaseList::configureObjects(themeChanged);
  if (themeChanged)
    KSim::ThemeLoader::self().themeColours(this);

  m_sysinfo->createView();
  maskMainView();

  m_topLevel->reparse();

  if (emitReload)
    emit reload();

  kdDebug(2003) << "new view created" << endl;
}

void KSim::MainView::addPlugins()
{
  TQStringList locatedFiles = TDEGlobal::dirs()->findAllResources("data", "ksim/monitors/*.desktop");
  TQStringList::ConstIterator it;
  for (it = locatedFiles.begin(); it != locatedFiles.end(); ++it)
  {
    const KDesktopFile kdf(*it, true);
    addPlugin(kdf);
  }
}

void KSim::MainView::addPlugin(const KDesktopFile &file, bool force)
{
  if (force || m_config->enabledMonitor(file.readEntry("X-KSIM-LIBRARY")))
    KSim::PluginLoader::self().loadPlugin(file);
}

// provided for convenience
void KSim::MainView::removePlugin(const KDesktopFile &file)
{
  KSim::PluginLoader::self().unloadPlugin(file.readEntry("X-KSIM-LIBRARY").local8Bit());
}

void KSim::MainView::addMonitor(const KSim::Plugin &plugin)
{
  if (!plugin.view())
    return;

  plugin.view()->reparent(this, 0, TQPoint(0, 0), true);
  KSim::ThemeLoader::self().themeColours(plugin.view());

//  int location = m_config->monitorLocation(plugin.libName());
//  if (location == -1)
//    location = m_oldLocation + 1;
//  else {
//    location += 1;
//    if (location > m_oldLocation)
//      location = m_oldLocation + 1;
//  }

//  kdDebug(2003) << "m_oldLocation: " << m_oldLocation << endl;
//  kdDebug(2003) << "location: " << location << endl;
  m_pluginLayout->addWidget(plugin.view());
  connect(plugin.view(), TQT_SIGNAL(runCommand(const TQCString &)),
     TQT_SLOT(runCommand(const TQCString &)));

//  if (location > m_oldLocation)
//    m_oldLocation = location;
}

void KSim::MainView::runCommand(const TQCString &name)
{
  if (name.isNull())
    return;

  kdDebug(2003) << "runCommand(" << name.mid(5) << ")" << endl;
  TQString exec = m_config->monitorCommand(name.mid(5));
  kdDebug(2003) << "exec is " << exec << endl;
  KRun::runCommand(exec);
}

void KSim::MainView::preferences()
{
  if (m_prefDialog == 0L) {
    m_prefDialog = new KSim::ConfigDialog(m_config, this, "m_prefDialog");
    connect(m_prefDialog, TQT_SIGNAL(reparse(bool, const KSim::ChangedPluginList &)),
       this, TQT_SLOT(reparseConfig(bool, const KSim::ChangedPluginList &)));
  }

  m_prefDialog->exec();
  destroyPref();
}

void KSim::MainView::resizeEvent(TQResizeEvent *re)
{
  TQWidget::resizeEvent(re);
  m_maskTimer.start(0, true);
}

void KSim::MainView::paletteChange(const TQPalette &)
{
  // Call true here to fool the KSim::Base pointers to
  // think our theme changed, we can afford todo this
  // since the palette doesn't change often.
  KSim::BaseList::configureObjects(true);

  // Probably find a better way to do this
  KSim::PluginList::ConstIterator it;
  const KSim::PluginList &pluginList = KSim::PluginLoader::self().pluginList();
  for (it = pluginList.begin(); it != pluginList.end(); ++it)
    KSim::ThemeLoader::self().themeColours((*it).view());
}

void KSim::MainView::destroyPref()
{
  if (m_prefDialog != 0L) {
    kdDebug(2003) << "deleting KSimPref" << endl;
    delete m_prefDialog;
    m_prefDialog = 0L;
  }
}

TQSize KSim::MainView::sizeHint(KPanelExtension::Position p, TQSize) const
{
  int width = 0;
  int height = 0;
  
  TQLayoutItem *child;
  for( TQLayoutIterator it = m_pluginLayout->iterator();
       (child = it.current()) != 0; ++it)
  {
     TQSize sz = child->minimumSize();
     if ((p == KPanelExtension::Right) || (p == KPanelExtension::Left))
     {
        width = TQMAX(width, sz.width());
        height += sz.height();
     }
     else
     {
        width += sz.width();
        height = TQMAX(height, sz.height());
     }
  }

  width += m_leftFrame->minimumWidth() + m_rightFrame->minimumWidth();
  height += m_topFrame->minimumHeight() + m_bottomFrame->minimumHeight();

  return TQSize(width, height);
}

void KSim::MainView::positionChange(Qt::Orientation o)
{
  if (o == Qt::Vertical)
     m_pluginLayout->setDirection(TQBoxLayout::TopToBottom);
  else
     m_pluginLayout->setDirection(TQBoxLayout::LeftToRight);
}