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

ark

  • sources
  • kde-4.14
  • kdeutils
  • ark
  • app
mainwindow.cpp
Go to the documentation of this file.
1 /*
2  * ark -- archiver for the KDE project
3  *
4  * Copyright (C) 2002-2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
5  * Copyright (C) 2003: Helio Chissini de Castro <helio@conectiva.com>
6  * Copyright (C) 2007 Henrique Pinto <henrique.pinto@kdemail.net>
7  * Copyright (C) 2008 Harald Hvaal <haraldhv@stud.ntnu.no>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  */
24 #include "mainwindow.h"
25 #include "kerfuffle/archive.h"
26 #include "part/interface.h"
27 
28 #include <KPluginLoader>
29 #include <KPluginFactory>
30 #include <KMessageBox>
31 #include <KApplication>
32 #include <KLocale>
33 #include <KActionCollection>
34 #include <KStandardAction>
35 #include <KFileDialog>
36 #include <KRecentFilesAction>
37 #include <KGlobal>
38 #include <KDebug>
39 #include <KEditToolBar>
40 #include <KShortcutsDialog>
41 #include <KXMLGUIFactory>
42 
43 #include <QDragEnterEvent>
44 #include <QDragMoveEvent>
45 #include <QWeakPointer>
46 
47 static bool isValidArchiveDrag(const QMimeData *data)
48 {
49  return ((data->hasUrls()) && (data->urls().count() == 1));
50 }
51 
52 MainWindow::MainWindow(QWidget *)
53  : KParts::MainWindow()
54 {
55  setXMLFile(QLatin1String( "arkui.rc" ));
56 
57  setupActions();
58  statusBar();
59 
60  if (!initialGeometrySet()) {
61  resize(640, 480);
62  }
63  setAutoSaveSettings(QLatin1String( "MainWindow" ));
64 
65  setAcceptDrops(true);
66 }
67 
68 MainWindow::~MainWindow()
69 {
70  if (m_recentFilesAction) {
71  m_recentFilesAction->saveEntries(KGlobal::config()->group("Recent Files"));
72  }
73 
74  guiFactory()->removeClient(m_part);
75  delete m_part;
76  m_part = 0;
77 }
78 
79 void MainWindow::dragEnterEvent(QDragEnterEvent * event)
80 {
81  kDebug() << event;
82 
83  Interface *iface = qobject_cast<Interface*>(m_part);
84  if (iface->isBusy()) {
85  return;
86  }
87 
88  if ((event->source() == NULL) &&
89  (isValidArchiveDrag(event->mimeData()))) {
90  event->acceptProposedAction();
91  }
92  return;
93 }
94 
95 void MainWindow::dropEvent(QDropEvent * event)
96 {
97  kDebug() << event;
98 
99  Interface *iface = qobject_cast<Interface*>(m_part);
100  if (iface->isBusy()) {
101  return;
102  }
103 
104  if ((event->source() == NULL) &&
105  (isValidArchiveDrag(event->mimeData()))) {
106  event->acceptProposedAction();
107  }
108 
109  //TODO: if this call provokes a message box the drag will still be going
110  //while the box is onscreen. looks buggy, do something about it
111  openUrl(event->mimeData()->urls().at(0));
112 }
113 
114 void MainWindow::dragMoveEvent(QDragMoveEvent * event)
115 {
116  kDebug() << event;
117 
118  Interface *iface = qobject_cast<Interface*>(m_part);
119  if (iface->isBusy()) {
120  return;
121  }
122 
123  if ((event->source() == NULL) &&
124  (isValidArchiveDrag(event->mimeData()))) {
125  event->acceptProposedAction();
126  }
127 }
128 
129 bool MainWindow::loadPart()
130 {
131  KPluginLoader loader(QLatin1String( "arkpart" ));
132  KPluginFactory *factory = loader.factory();
133  if (factory) {
134  m_part = static_cast<KParts::ReadWritePart*>(factory->create<KParts::ReadWritePart>(this));
135  }
136  if (!factory || !m_part) {
137  KMessageBox::error(this, i18n("Unable to find Ark's KPart component, please check your installation."));
138  kWarning() << "Error loading Ark KPart: " << loader.errorString();
139  return false;
140  }
141 
142  m_part->setObjectName( QLatin1String("ArkPart" ));
143  setCentralWidget(m_part->widget());
144  createGUI(m_part);
145 
146  connect(m_part, SIGNAL(busy()), this, SLOT(updateActions()));
147  connect(m_part, SIGNAL(ready()), this, SLOT(updateActions()));
148  connect(m_part, SIGNAL(quit()), this, SLOT(quit()));
149 
150  return true;
151 }
152 
153 void MainWindow::setupActions()
154 {
155  m_newAction = KStandardAction::openNew(this, SLOT(newArchive()), actionCollection());
156  m_openAction = KStandardAction::open(this, SLOT(openArchive()), actionCollection());
157  KStandardAction::quit(this, SLOT(quit()), actionCollection());
158 
159  m_recentFilesAction = KStandardAction::openRecent(this, SLOT(openUrl(KUrl)), actionCollection());
160  m_recentFilesAction->setToolBarMode(KRecentFilesAction::MenuMode);
161  m_recentFilesAction->setToolButtonPopupMode(QToolButton::DelayedPopup);
162  m_recentFilesAction->setIconText(i18nc("action, to open an archive", "Open"));
163  m_recentFilesAction->setStatusTip(i18n("Click to open an archive, click and hold to open a recently-opened archive"));
164  m_recentFilesAction->setToolTip(i18n("Open an archive"));
165  m_recentFilesAction->loadEntries(KGlobal::config()->group("Recent Files"));
166  connect(m_recentFilesAction, SIGNAL(triggered()),
167  this, SLOT(openArchive()));
168 
169  createStandardStatusBarAction();
170 
171  KStandardAction::configureToolbars(this, SLOT(editToolbars()), actionCollection());
172  KStandardAction::keyBindings(this, SLOT(editKeyBindings()), actionCollection());
173 }
174 
175 void MainWindow::updateActions()
176 {
177  Interface *iface = qobject_cast<Interface*>(m_part);
178  m_newAction->setEnabled(!iface->isBusy());
179  m_openAction->setEnabled(!iface->isBusy());
180  m_recentFilesAction->setEnabled(!iface->isBusy());
181 }
182 
183 void MainWindow::editKeyBindings()
184 {
185  KShortcutsDialog dlg(KShortcutsEditor::AllActions, KShortcutsEditor::LetterShortcutsAllowed, this);
186  dlg.addCollection(actionCollection());
187  dlg.addCollection(m_part->actionCollection());
188 
189  dlg.configure();
190 }
191 
192 void MainWindow::editToolbars()
193 {
194  saveMainWindowSettings(KGlobal::config()->group(QLatin1String("MainWindow")));
195 
196  QWeakPointer<KEditToolBar> dlg = new KEditToolBar(factory(), this);
197  dlg.data()->exec();
198 
199  createGUI(m_part);
200 
201  applyMainWindowSettings(KGlobal::config()->group(QLatin1String("MainWindow")));
202 
203  delete dlg.data();
204 }
205 
206 void MainWindow::openArchive()
207 {
208  Interface *iface = qobject_cast<Interface*>(m_part);
209  Q_ASSERT(iface);
210  const KUrl url = KFileDialog::getOpenUrl(KUrl("kfiledialog:///ArkOpenDir"),
211  Kerfuffle::supportedMimeTypes().join( QLatin1String( " " )),
212  this);
213  openUrl(url);
214 }
215 
216 void MainWindow::openUrl(const KUrl& url)
217 {
218  if (!url.isEmpty()) {
219  m_part->setArguments(m_openArgs);
220 
221  if (m_part->openUrl(url)) {
222  m_recentFilesAction->addUrl(url);
223  } else {
224  m_recentFilesAction->removeUrl(url);
225  }
226  }
227 }
228 
229 void MainWindow::setShowExtractDialog(bool option)
230 {
231  if (option) {
232  m_openArgs.metaData()[QLatin1String( "showExtractDialog" )] = QLatin1String( "true" );
233  } else {
234  m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
235  }
236 }
237 
238 void MainWindow::quit()
239 {
240  close();
241 }
242 
243 void MainWindow::newArchive()
244 {
245  Interface *iface = qobject_cast<Interface*>(m_part);
246  Q_ASSERT(iface);
247 
248  const QStringList mimeTypes = Kerfuffle::supportedWriteMimeTypes();
249 
250  kDebug() << "Supported mimetypes are" << mimeTypes.join( QLatin1String( " " ));
251 
252  const KUrl saveFileUrl =
253  KFileDialog::getSaveUrl(KUrl("kfiledialog:///ArkNewDir"),
254  mimeTypes.join(QLatin1String(" ")));
255 
256  m_openArgs.metaData()[QLatin1String( "createNewArchive" )] = QLatin1String( "true" );
257 
258  openUrl(saveFileUrl);
259 
260  m_openArgs.metaData().remove(QLatin1String( "showExtractDialog" ));
261  m_openArgs.metaData().remove(QLatin1String( "createNewArchive" ));
262 }
QDropEvent::source
QWidget * source() const
QWidget
MainWindow::dragEnterEvent
void dragEnterEvent(class QDragEnterEvent *event)
Definition: mainwindow.cpp:79
QDropEvent::mimeData
const QMimeData * mimeData() const
MainWindow::loadPart
bool loadPart()
Definition: mainwindow.cpp:129
Kerfuffle::supportedWriteMimeTypes
QStringList supportedWriteMimeTypes()
Definition: archive.cpp:306
QDragMoveEvent
QWeakPointer::data
T * data() const
archive.h
QStringList::join
QString join(const QString &separator) const
QMimeData
isValidArchiveDrag
static bool isValidArchiveDrag(const QMimeData *data)
Definition: mainwindow.cpp:47
Kerfuffle::supportedMimeTypes
QStringList supportedMimeTypes()
Definition: archive.cpp:279
interface.h
QDropEvent
mainwindow.h
MainWindow::MainWindow
MainWindow(QWidget *parent=0)
Definition: mainwindow.cpp:52
QStringList
Interface
Definition: interface.h:27
Interface::isBusy
virtual bool isBusy() const =0
QMimeData::hasUrls
bool hasUrls() const
QDragEnterEvent
QLatin1String
QMimeData::urls
QList< QUrl > urls() const
MainWindow::setShowExtractDialog
void setShowExtractDialog(bool)
Definition: mainwindow.cpp:229
MainWindow::dragMoveEvent
void dragMoveEvent(class QDragMoveEvent *event)
Definition: mainwindow.cpp:114
QWeakPointer
MainWindow::openUrl
void openUrl(const KUrl &url)
Definition: mainwindow.cpp:216
MainWindow::~MainWindow
~MainWindow()
Definition: mainwindow.cpp:68
MainWindow
Definition: mainwindow.h:31
QAction::setEnabled
void setEnabled(bool)
MainWindow::dropEvent
void dropEvent(class QDropEvent *event)
Definition: mainwindow.cpp:95
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:37 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ark

Skip menu "ark"
  • 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