• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

filelight

  • sources
  • kde-4.14
  • kdeutils
  • filelight
  • src
  • app
mainWindow.cpp
Go to the documentation of this file.
1 /***********************************************************************
2 * Copyright 2003-2004 Max Howell <max.howell@methylblue.com>
3 * Copyright 2008-2009 Martin Sandsmark <martin.sandsmark@kde.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License or (at your option) version 3 or any later version
9 * accepted by the membership of KDE e.V. (or its successor approved
10 * by the membership of KDE e.V.), which shall act as a proxy
11 * defined in Section 14 of version 3 of the license.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 
22 #include "mainWindow.h"
23 #include "part/part.h"
24 #include "historyAction.h"
25 
26 #include <cstdlib> //std::exit()
27 #include <KApplication> //setupActions()
28 #include <KComboBox> //locationbar
29 #include <KHistoryComboBox>
30 #include <KRecentFilesAction>
31 #include <KConfig>
32 #include <KDirSelectDialog> //slotScanFolder
33 #include <KEditToolBar> //for editToolbar dialog
34 #include <QLineEdit>
35 #include <KStandardShortcut>
36 #include <KFileDialog>
37 #include <KLibLoader>
38 #include <KLocale>
39 #include <KMessageBox>
40 #include <KShell>
41 #include <KStatusBar>
42 #include <KToolBar>
43 #include <KUrl>
44 #include <KUrlCompletion> //locationbar
45 #include <QObject>
46 #include <QToolTip>
47 #include <KGlobal>
48 #include <KConfigGroup>
49 #include <KShortcutsDialog>
50 #include <KSharedConfig>
51 #include <KStandardAction>
52 #include <KActionCollection>
53 
54 namespace Filelight {
55 
56 MainWindow::MainWindow() : KParts::MainWindow(), m_part(0)
57 {
58 // setXMLFile("filelightui.rc");
59  KPluginFactory *factory = KPluginLoader(QLatin1String( "filelightpart" )).factory();
60 
61  if (!factory) {
62  KMessageBox::error(this, i18n("Unable to load the Filelight Part.\nPlease make sure Filelight was correctly installed."));
63  std::exit(1);
64  return;
65  }
66 
67  m_part = static_cast<Part *>(factory->create<KParts::ReadOnlyPart>(this));
68 
69  if (m_part) {
70  setStandardToolBarMenuEnabled(true);
71  setupActions();
72  createGUI(m_part);
73  setCentralWidget(m_part->widget());
74 
75  stateChanged(QLatin1String( "scan_failed" )); //bah! doesn't affect the parts' actions, should I add them to the actionCollection here?
76 
77  connect(m_part, SIGNAL(started(KIO::Job*)), SLOT(scanStarted()));
78  connect(m_part, SIGNAL(completed()), SLOT(scanCompleted()));
79  connect(m_part, SIGNAL(canceled(QString)), SLOT(scanFailed()));
80 
81  connect(m_part, SIGNAL(canceled(QString)), m_histories, SLOT(stop()));
82  connect(BrowserExtension::childObject(m_part), SIGNAL(openUrlNotify()), SLOT(urlAboutToChange()));
83 
84  const KConfigGroup config = KGlobal::config()->group("general");
85  m_combo->setHistoryItems(config.readPathEntry("comboHistory", QStringList()));
86  } else {
87  KMessageBox::error(this, i18n("Unable to create part widget."));
88  std::exit(1);
89  }
90 
91  setAutoSaveSettings(QLatin1String( "window" ));
92 }
93 
94 inline void MainWindow::setupActions() //singleton function
95 {
96  KActionCollection *const ac = actionCollection();
97 
98  m_combo = new KHistoryComboBox(this);
99  m_combo->setCompletionObject(new KUrlCompletion(KUrlCompletion::DirCompletion));
100  m_combo->setAutoDeleteCompletionObject(true);
101  m_combo->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
102  m_combo->setDuplicatesEnabled(false);
103 
104  KStandardAction::open(this, SLOT(slotScanFolder()), ac);
105  KStandardAction::quit(this, SLOT(close()), ac);
106  KStandardAction::up(this, SLOT(slotUp()), ac);
107  KStandardAction::configureToolbars(this, SLOT(configToolbars()), ac);
108  KStandardAction::keyBindings(this, SLOT(configKeys()), ac);
109 
110  KAction* action;
111 
112  action = ac->addAction(QLatin1String( "scan_home" ), this, SLOT(slotScanHomeFolder()));
113  action->setText(i18n("Scan &Home Folder"));
114  action->setIcon(KIcon(QLatin1String( "user-home" )));
115  action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Home));
116 
117  action = ac->addAction(QLatin1String( "scan_root" ), this, SLOT(slotScanRootFolder()));
118  action->setText(i18n("Scan &Root Folder"));
119  action->setIcon(KIcon(QLatin1String( "folder-red" )));
120 
121  action = ac->addAction(QLatin1String( "scan_rescan" ), m_part, SLOT(rescan()));
122  action->setText(i18n("Rescan"));
123  action->setIcon(KIcon(QLatin1String( "view-refresh" )));
124  action->setShortcut(KStandardShortcut::reload());
125 
126 
127  action = ac->addAction(QLatin1String( "scan_stop" ), this, SLOT(slotAbortScan()));
128  action->setText(i18n("Stop"));
129  action->setIcon(KIcon(QLatin1String( "process-stop" )));
130  action->setShortcut(Qt::Key_Escape);
131 
132  action = ac->addAction(QLatin1String( "go" ), m_combo, SIGNAL(returnPressed()));
133  action->setText(i18n("Go"));
134  action->setIcon(KIcon(QLatin1String( "go-jump-locationbar" )));
135 
136  action = ac->addAction(QLatin1String( "location_bar" ), 0, 0);
137  action->setText(i18n("Location Bar"));
138  action->setDefaultWidget(m_combo);
139 
140  action = ac->addAction(QLatin1String( "scan_folder" ), this, SLOT(slotScanFolder()));
141  action->setText(i18n("Scan Folder"));
142  action->setIcon(KIcon(QLatin1String( "folder" )));
143 
144  m_recentScans = new KRecentFilesAction(i18n("&Recent Scans"), ac);
145  m_recentScans->setMaxItems(8);
146 
147  m_histories = new HistoryCollection(ac, this);
148 
149  m_recentScans->loadEntries(KGlobal::config()->group("general"));
150 
151  connect(m_recentScans, SIGNAL(urlSelected(KUrl)), SLOT(slotScanUrl(KUrl)));
152  connect(m_combo, SIGNAL(returnPressed()), SLOT(slotComboScan()));
153  connect(m_histories, SIGNAL(activated(KUrl)), SLOT(slotScanUrl(KUrl)));
154 }
155 
156 void MainWindow::closeEvent(QCloseEvent *event)
157 {
158  KConfigGroup config = KGlobal::config()->group("general");
159 
160  m_recentScans->saveEntries(config);
161  config.writePathEntry("comboHistory", m_combo->historyItems());
162  config.sync();
163 
164  KParts::MainWindow::closeEvent(event);
165 }
166 
167 inline void MainWindow::configToolbars() //slot
168 {
169  KEditToolBar dialog(factory(), this);
170 
171  if (dialog.exec()) //krazy:exclude=crashy
172  {
173  createGUI(m_part);
174  applyMainWindowSettings(KGlobal::config()->group("window"));
175  }
176 }
177 
178 inline void MainWindow::configKeys() //slot
179 {
180  KShortcutsDialog::configure(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this, true);
181 }
182 
183 inline void MainWindow::slotScanFolder()
184 {
185  slotScanUrl(KFileDialog::getExistingDirectoryUrl(m_part->url(), this, i18n("Select Folder to Scan")));
186 }
187 
188 inline void MainWindow::slotScanHomeFolder() {
189  slotScanPath(QDir::homePath());
190 }
191 inline void MainWindow::slotScanRootFolder() {
192  slotScanPath(QDir::rootPath());
193 }
194 inline void MainWindow::slotUp() {
195  slotScanUrl(m_part->url().upUrl());
196 }
197 
198 inline void MainWindow::slotComboScan()
199 {
200  QString path = m_combo->lineEdit()->text();
201 
202  KUrl url = KUrl(path);
203 
204  if (url.isRelative())
205  path = QLatin1String( "~/" ) + path; // KUrlCompletion completes relative to ~, not CWD
206 
207  path = KShell::tildeExpand(path);
208 
209  if (slotScanPath(path))
210  m_combo->addToHistory(path);
211 }
212 
213 inline bool MainWindow::slotScanPath(const QString &path)
214 {
215  return slotScanUrl(KUrl(path));
216 }
217 
218 bool MainWindow::slotScanUrl(const KUrl &url)
219 {
220  const KUrl oldUrl = m_part->url();
221 
222  if (m_part->openUrl(url))
223  {
224  m_histories->push(oldUrl);
225  return true;
226  }
227  else
228  return false;
229 }
230 
231 inline void MainWindow::slotAbortScan()
232 {
233  if (m_part->closeUrl()) action("scan_stop")->setEnabled(false);
234 }
235 
236 inline void MainWindow::scanStarted()
237 {
238  stateChanged(QLatin1String( "scan_started" ));
239  m_combo->clearFocus();
240 }
241 
242 inline void MainWindow::scanFailed()
243 {
244  stateChanged(QLatin1String( "scan_failed" ));
245  qobject_cast<KAction*>(action("go_up"))->setHelpText(QString());
246  m_combo->lineEdit()->clear();
247 }
248 
249 void MainWindow::scanCompleted()
250 {
251  KAction *goUp = qobject_cast<KAction *>(action("go_up"));
252  const KUrl url = m_part->url();
253 
254  stateChanged(QLatin1String( "scan_complete" ));
255 
256  m_combo->lineEdit()->setText(m_part->prettyUrl());
257 
258  if (url.path(KUrl::LeaveTrailingSlash) == QLatin1String( "/" )) {
259  goUp->setEnabled(false);
260  goUp->setHelpText(QString());
261  }
262  else
263  goUp->setHelpText(url.upUrl().path(KUrl::LeaveTrailingSlash));
264 
265  m_recentScans->addUrl(url); //FIXME doesn't set the tick
266 }
267 
268 inline void MainWindow::urlAboutToChange()
269 {
270  //called when part's URL is about to change internally
271  //the part will then create the Map and emit completed()
272 
273  m_histories->push(m_part->url());
274 }
275 
276 
277 /**********************************************
278  SESSION MANAGEMENT
279  **********************************************/
280 
281 void MainWindow::saveProperties(KConfigGroup &configgroup) //virtual
282 {
283  m_histories->save(configgroup);
284  configgroup.writeEntry("currentMap", m_part->url().path());
285 }
286 
287 void MainWindow::readProperties(const KConfigGroup &configgroup) //virtual
288 {
289  m_histories->restore(configgroup);
290  slotScanPath(configgroup.group("General").readEntry("currentMap", QString()));
291 }
292 
293 } //namespace Filelight
294 
295 #include "mainWindow.moc"
Filelight::MainWindow::closeEvent
virtual void closeEvent(QCloseEvent *event)
Definition: mainWindow.cpp:156
QSizePolicy
HistoryCollection::save
void save(KConfigGroup &configgroup)
Definition: historyAction.cpp:100
QDir::homePath
QString homePath()
Filelight::Part::closeUrl
virtual bool closeUrl()
Definition: part.cpp:198
Filelight::Part::prettyUrl
QString prettyUrl() const
Definition: part.h:62
mainWindow.h
QCloseEvent
Filelight::MainWindow
Definition: mainWindow.h:38
Filelight::MainWindow::MainWindow
MainWindow()
Definition: mainWindow.cpp:56
Filelight::Part::openUrl
virtual bool openUrl(const KUrl &)
Definition: part.cpp:147
QString
QStringList
Filelight::MainWindow::readProperties
virtual void readProperties(const KConfigGroup &)
Definition: mainWindow.cpp:287
QLatin1String
QKeySequence
QDir::rootPath
QString rootPath()
KAction
Filelight::MainWindow::saveProperties
virtual void saveProperties(KConfigGroup &)
Definition: mainWindow.cpp:281
Filelight::Part
Definition: part.h:50
HistoryCollection::push
void push(const KUrl &)
Definition: historyAction.cpp:74
HistoryCollection
Definition: historyAction.h:60
HistoryCollection::restore
void restore(const KConfigGroup &configgroup)
Definition: historyAction.cpp:106
part.h
historyAction.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:32 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

filelight

Skip menu "filelight"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal