summaryrefslogtreecommitdiffstats
path: root/ksim/monitors/filesystem/fsystemconfig.cpp
blob: 7afd53aae388e1598a188ba11d108a27673d6c5e (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
/*  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 "fsystemconfig.h"
#include "fsystemconfig.moc"

#include <tdeconfig.h>
#include <tdelocale.h>
#include <kiconloader.h>
#include <tdelistview.h>
#include <tdeapplication.h>
#include <knuminput.h>
#include <kdebug.h>

#include <tqcheckbox.h>
#include <tqlayout.h>
#include <tqtooltip.h>
#include <tqlabel.h>
#include <tqwhatsthis.h>

class FSysViewItem : public TQCheckListItem
{
  public:
    FSysViewItem(TQListView *parent, const TQString &text1,
       const TQString &text2, const TQString &text3)
        : TQCheckListItem(parent, text1, CheckBox)
    {
      setText(1, text2);
      setText(2, text3);
    }
};

FsystemConfig::FsystemConfig(KSim::PluginObject *parent, const char *name)
   : KSim::PluginPage(parent, name)
{
  m_mainLayout = new TQGridLayout(this);
  m_mainLayout->setSpacing(6);

  m_availableMounts = new TDEListView(this);
  m_availableMounts->addColumn(i18n("Mounted Partition"));
  m_availableMounts->addColumn(i18n("Device"));
  m_availableMounts->addColumn(i18n("Type"));
  m_mainLayout->addMultiCellWidget(m_availableMounts, 0, 0, 0, 3);

  m_showPercentage = new TQCheckBox(this);
  m_showPercentage->setText(i18n("Show percentage"));
  m_mainLayout->addMultiCellWidget(m_showPercentage, 1, 1, 0, 3);

  m_splitNames = new TQCheckBox(this);
  m_splitNames->setText(i18n("Display short mount point names"));
  TQWhatsThis::add(m_splitNames, i18n("This option shortens the text"
     " to shrink down a mount point. E.G: a mount point /home/myuser"
     " would become myuser."));
  m_mainLayout->addMultiCellWidget(m_splitNames, 2, 2, 0, 3);

  m_intervalLabel = new TQLabel(this);
  m_intervalLabel->setText( i18n("Update interval:"));
  m_intervalLabel->setSizePolicy(TQSizePolicy(TQSizePolicy::Fixed,
     TQSizePolicy::Fixed));
  m_mainLayout->addMultiCellWidget(m_intervalLabel, 3, 3, 0, 0);

  m_updateTimer = new KIntSpinBox(this);
  m_updateTimer->setValue(60);
  TQToolTip::add(m_updateTimer, i18n("0 means no update"));
  m_mainLayout->addMultiCellWidget(m_updateTimer, 3, 3, 1, 1);

  TQLabel *intervalLabel = new TQLabel(this);
  intervalLabel->setText(i18n("seconds"));
  intervalLabel->setSizePolicy(TQSizePolicy(TQSizePolicy::Fixed,
     TQSizePolicy::Fixed));
  m_mainLayout->addMultiCellWidget(intervalLabel, 3, 3, 2, 2);

  m_entries = FilesystemStats::readEntries();
  getStats();
}

FsystemConfig::~FsystemConfig()
{
}

void FsystemConfig::readConfig()
{
  config()->setGroup("Fsystem");
  m_showPercentage->setChecked(config()->readBoolEntry("ShowPercentage", true));
  m_updateTimer->setValue(config()->readNumEntry("updateValue", 60));
  m_splitNames->setChecked(config()->readBoolEntry("ShortenEntries", false));

  if (!m_availableMounts->childCount())
    return;

  TQStringList list = config()->readListEntry("mountEntries");
  for (TQListViewItemIterator it(m_availableMounts); it.current(); ++it) {
    TQString string = it.current()->text(0) + ":" + splitString(it.current()->text(0));
    static_cast<TQCheckListItem *>(it.current())->setOn(list.contains(string) > 0);
  }
}

void FsystemConfig::saveConfig()
{
  config()->setGroup("Fsystem");
  config()->writeEntry("ShowPercentage", m_showPercentage->isChecked());
  config()->writeEntry("updateValue", m_updateTimer->value());
  config()->writeEntry("ShortenEntries", m_splitNames->isChecked());

  TQStringList list;
  for (TQListViewItemIterator it(m_availableMounts); it.current(); ++it) {
    if (static_cast<TQCheckListItem *>(it.current())->isOn())
      list.append(it.current()->text(0) + ":" + splitString(it.current()->text(0)));
  }

  config()->writeEntry("mountEntries", list);
}

void FsystemConfig::showEvent(TQShowEvent *)
{
  // FIXME: Maybe this is the slow method of doing this?
  // Eitherway, i need to find a way to only update the list
  // if the amount of mounted partitions has changed
  FilesystemStats::List entries = FilesystemStats::readEntries();
  if ( entries.count() == m_entries.count() )
    return;

  m_entries = entries;

  // Update the entries to take into account
  // any mounted/unmounted filesystems
  m_availableMounts->clear();
  getStats();
}

void FsystemConfig::getStats()
{
  int total = 0;
  int free = 0;

  FilesystemStats::List::ConstIterator it;
  for ( it = m_entries.begin(); it != m_entries.end(); ++it )
  {
    if ( !FilesystemStats::readStats( ( *it ).dir, total, free ) )
      continue;

    if ( !m_availableMounts->findItem( ( *it ).dir, 0 ) )
    {
      (void) new FSysViewItem( m_availableMounts, ( *it ).dir,
         ( *it ).fsname, ( *it ).type );
    }
  }

  if (!m_availableMounts->childCount())
    return;

  config()->setGroup("Fsystem");
  TQStringList list = config()->readListEntry("mountEntries");
  for (TQListViewItemIterator it(m_availableMounts); it.current(); ++it) {
    TQString string = it.current()->text(0) + ":" + splitString(it.current()->text(0));
    static_cast<TQCheckListItem *>(it.current())->setOn(list.contains(string) > 0);
  }
}

TQString FsystemConfig::splitString(const TQString &string) const
{
  if (string == "/" || !m_splitNames->isChecked())
    return string;

  int location = string.findRev("/");
  TQString newString(string);
  return newString.remove(0, location + 1);
}