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

filelight

  • sources
  • kde-4.12
  • 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 bool MainWindow::queryExit()
157 {
158  if (!m_part) //apparently std::exit() still calls this function, and abort() causes a crash..
159  return true;
160 
161  KConfigGroup config = KGlobal::config()->group("general");
162 
163  m_recentScans->saveEntries(config);
164  config.writePathEntry("comboHistory", m_combo->historyItems());
165  config.sync();
166 
167  return true;
168 }
169 
170 inline void MainWindow::configToolbars() //slot
171 {
172  KEditToolBar dialog(factory(), this);
173 
174  if (dialog.exec()) //krazy:exclude=crashy
175  {
176  createGUI(m_part);
177  applyMainWindowSettings(KGlobal::config()->group("window"));
178  }
179 }
180 
181 inline void MainWindow::configKeys() //slot
182 {
183  KShortcutsDialog::configure(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, this, true);
184 }
185 
186 inline void MainWindow::slotScanFolder()
187 {
188  slotScanUrl(KFileDialog::getExistingDirectoryUrl(m_part->url(), this, i18n("Select Folder to Scan")));
189 }
190 
191 inline void MainWindow::slotScanHomeFolder() {
192  slotScanPath(QDir::homePath());
193 }
194 inline void MainWindow::slotScanRootFolder() {
195  slotScanPath(QDir::rootPath());
196 }
197 inline void MainWindow::slotUp() {
198  slotScanUrl(m_part->url().upUrl());
199 }
200 
201 inline void MainWindow::slotComboScan()
202 {
203  QString path = m_combo->lineEdit()->text();
204 
205  KUrl url = KUrl(path);
206 
207  if (url.isRelative())
208  path = QLatin1String( "~/" ) + path; // KUrlCompletion completes relative to ~, not CWD
209 
210  path = KShell::tildeExpand(path);
211 
212  if (slotScanPath(path))
213  m_combo->addToHistory(path);
214 }
215 
216 inline bool MainWindow::slotScanPath(const QString &path)
217 {
218  return slotScanUrl(KUrl(path));
219 }
220 
221 bool MainWindow::slotScanUrl(const KUrl &url)
222 {
223  const KUrl oldUrl = m_part->url();
224 
225  if (m_part->openUrl(url))
226  {
227  m_histories->push(oldUrl);
228  return true;
229  }
230  else
231  return false;
232 }
233 
234 inline void MainWindow::slotAbortScan()
235 {
236  if (m_part->closeUrl()) action("scan_stop")->setEnabled(false);
237 }
238 
239 inline void MainWindow::scanStarted()
240 {
241  stateChanged(QLatin1String( "scan_started" ));
242  m_combo->clearFocus();
243 }
244 
245 inline void MainWindow::scanFailed()
246 {
247  stateChanged(QLatin1String( "scan_failed" ));
248  qobject_cast<KAction*>(action("go_up"))->setHelpText(QString());
249  m_combo->lineEdit()->clear();
250 }
251 
252 void MainWindow::scanCompleted()
253 {
254  KAction *goUp = qobject_cast<KAction *>(action("go_up"));
255  const KUrl url = m_part->url();
256 
257  stateChanged(QLatin1String( "scan_complete" ));
258 
259  m_combo->lineEdit()->setText(m_part->prettyUrl());
260 
261  if (url.path(KUrl::LeaveTrailingSlash) == QLatin1String( "/" )) {
262  goUp->setEnabled(false);
263  goUp->setHelpText(QString());
264  }
265  else
266  goUp->setHelpText(url.upUrl().path(KUrl::LeaveTrailingSlash));
267 
268  m_recentScans->addUrl(url); //FIXME doesn't set the tick
269 }
270 
271 inline void MainWindow::urlAboutToChange()
272 {
273  //called when part's URL is about to change internally
274  //the part will then create the Map and emit completed()
275 
276  m_histories->push(m_part->url());
277 }
278 
279 
280 /**********************************************
281  SESSION MANAGEMENT
282  **********************************************/
283 
284 void MainWindow::saveProperties(KConfigGroup &configgroup) //virtual
285 {
286  m_histories->save(configgroup);
287  configgroup.writeEntry("currentMap", m_part->url().path());
288 }
289 
290 void MainWindow::readProperties(const KConfigGroup &configgroup) //virtual
291 {
292  m_histories->restore(configgroup);
293  slotScanPath(configgroup.group("General").readEntry("currentMap", QString()));
294 }
295 
296 } //namespace Filelight
297 
298 #include "mainWindow.moc"
HistoryCollection::save
void save(KConfigGroup &configgroup)
Definition: historyAction.cpp:100
Filelight::Part::closeUrl
virtual bool closeUrl()
Definition: part.cpp:198
Filelight::Part::prettyUrl
QString prettyUrl() const
Definition: part.h:62
mainWindow.h
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
Filelight::MainWindow::readProperties
virtual void readProperties(const KConfigGroup &)
Definition: mainWindow.cpp:290
KAction
Filelight::MainWindow::saveProperties
virtual void saveProperties(KConfigGroup &)
Definition: mainWindow.cpp:284
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
Filelight::MainWindow::queryExit
virtual bool queryExit()
Definition: mainWindow.cpp:156
part.h
historyAction.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:08:07 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
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • 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