• 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
  • part
arkviewer.cpp
Go to the documentation of this file.
1 /*
2  * ark: A program for modifying archives via a GUI.
3  *
4  * Copyright (C) 2004-2008 Henrique Pinto <henrique.pinto@kdemail.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  */
21 
22 #include "arkviewer.h"
23 
24 #include <KLocale>
25 #include <KMimeTypeTrader>
26 #include <KMimeType>
27 #include <KDebug>
28 #include <KUrl>
29 #include <KGlobal>
30 #include <KIconLoader>
31 #include <KVBox>
32 #include <KMessageBox>
33 #include <KProgressDialog>
34 #include <KPushButton>
35 #include <KRun>
36 #include <KIO/NetAccess>
37 #include <khtml_part.h>
38 
39 #include <QHBoxLayout>
40 #include <QFile>
41 #include <QFrame>
42 #include <QLabel>
43 
44 ArkViewer::ArkViewer(QWidget * parent, Qt::WFlags flags)
45  : KDialog(parent, flags)
46 {
47  setButtons(Close);
48  m_widget = new KVBox(this);
49  m_widget->layout()->setSpacing(10);
50 
51  setMainWidget(m_widget);
52 
53  connect(this, SIGNAL(finished()), SLOT(dialogClosed()));
54 }
55 
56 ArkViewer::~ArkViewer()
57 {
58 }
59 
60 void ArkViewer::dialogClosed()
61 {
62  KConfigGroup conf = KGlobal::config()->group("Viewer");
63  saveDialogSize(conf);
64 
65  if (m_part) {
66  KProgressDialog progressDialog
67  (this, i18n("Closing preview"),
68  i18n("Please wait while the preview is being closed..."));
69 
70  progressDialog.setMinimumDuration(500);
71  progressDialog.setModal(true);
72  progressDialog.setAllowCancel(false);
73  progressDialog.progressBar()->setRange(0, 0);
74 
75  // #261785: this preview dialog is not modal, so we need to delete
76  // the previewed file ourselves when the dialog is closed;
77  // we used to remove it at the end of ArkViewer::view() when
78  // QDialog::exec() was called instead of QDialog::show().
79  const QString previewedFilePath(m_part.data()->url().pathOrUrl());
80 
81  m_part.data()->closeUrl();
82 
83  if (!previewedFilePath.isEmpty()) {
84  QFile::remove(previewedFilePath);
85  }
86  }
87 }
88 
89 void ArkViewer::view(const QString& fileName, QWidget *parent)
90 {
91  KMimeType::Ptr mimeType = KMimeType::findByPath(fileName);
92  kDebug() << "MIME type" << mimeType->name();
93  KService::Ptr viewer = ArkViewer::getViewer(mimeType);
94 
95  const bool needsExternalViewer = (!viewer.isNull() &&
96  !viewer->hasServiceType(QLatin1String("KParts/ReadOnlyPart")));
97  if (needsExternalViewer) {
98  // We have already resolved the MIME type and the service above.
99  // So there is no point in using KRun::runUrl() which would need
100  // to do the same again.
101 
102  const KUrl::List fileUrlList = KUrl(fileName);
103  // The last argument (tempFiles) set to true means that the temporary
104  // file will be removed when the viewer application exits.
105  KRun::run(*viewer, fileUrlList, parent, true);
106  return;
107  }
108 
109  bool viewInInternalViewer = true;
110  if (viewer.isNull()) {
111  // No internal viewer available for the file. Ask the user if it
112  // should be previewed as text/plain.
113 
114  int response;
115  if (!mimeType->isDefault()) {
116  // File has a defined MIME type, and not the default
117  // application/octet-stream. So it could be viewable as
118  // plain text, ask the user.
119  response = KMessageBox::warningContinueCancel(parent,
120  i18n("The internal viewer cannot preview this type of file<nl/>(%1).<nl/><nl/>Do you want to try to view it as plain text?", mimeType->name()),
121  i18nc("@title:window", "Cannot Preview File"),
122  KGuiItem(i18nc("@action:button", "Preview as Text"), KIcon(QLatin1String("text-plain"))),
123  KStandardGuiItem::cancel(),
124  QString(QLatin1String("PreviewAsText_%1")).arg(mimeType->name()));
125  }
126  else {
127  // No defined MIME type, or the default application/octet-stream.
128  // There is still a possibility that it could be viewable as plain
129  // text, so ask the user. Not the same as the message/question
130  // above, because the wording and default are different.
131  response = KMessageBox::warningContinueCancel(parent,
132  i18n("The internal viewer cannot preview this unknown type of file.<nl/><nl/>Do you want to try to view it as plain text?"),
133  i18nc("@title:window", "Cannot Preview File"),
134  KGuiItem(i18nc("@action:button", "Preview as Text"), KIcon(QLatin1String("text-plain"))),
135  KStandardGuiItem::cancel(),
136  QString(),
137  KMessageBox::Dangerous);
138  }
139 
140  if (response == KMessageBox::Cancel) {
141  viewInInternalViewer = false;
142  }
143  else { // set for viewer later
144  mimeType = KMimeType::mimeType(QLatin1String("text/plain"));
145  }
146  }
147 
148  if (viewInInternalViewer) {
149  ArkViewer *internalViewer = new ArkViewer(parent, Qt::Window);
150  if (internalViewer->viewInInternalViewer(fileName, mimeType)) {
151  internalViewer->show();
152  // The internal viewer is showing the file, and will
153  // remove the temporary file in dialogClosed(). So there
154  // is no more to do here.
155  return;
156  }
157  else {
158  KMessageBox::sorry(parent, i18n("The internal viewer cannot preview this file."));
159  delete internalViewer;
160  }
161  }
162 
163  // Only get here if there is no internal viewer available or could be
164  // used for the file, and no external viewer was opened. Nothing can be
165  // done with the temporary file, so remove it now.
166  QFile::remove(fileName);
167 }
168 
169 void ArkViewer::keyPressEvent(QKeyEvent *event)
170 {
171  KPushButton *defButton = button(defaultButton());
172 
173  // Only handle the event the usual way if the default button has focus
174  // Otherwise, pressing enter on KatePart still closes the dialog, for example.
175  if ((defButton) && (defButton->hasFocus())) {
176  KDialog::keyPressEvent(event);
177  }
178 
179  event->accept();
180 }
181 
182 // This sets the default size of the dialog. It will only take effect in the case
183 // where there is no saved size in the config file - it sets the default values
184 // for KDialog::restoreDialogSize().
185 QSize ArkViewer::sizeHint() const
186 {
187  return QSize(560, 400);
188 }
189 
190 bool ArkViewer::viewInInternalViewer(const QString& fileName, const KMimeType::Ptr& mimeType)
191 {
192  const KUrl fileUrl(fileName);
193 
194  setCaption(fileUrl.fileName());
195  restoreDialogSize(KGlobal::config()->group("Viewer"));
196 
197  QFrame *header = new QFrame(m_widget);
198  QHBoxLayout *headerLayout = new QHBoxLayout(header);
199 
200  QLabel *iconLabel = new QLabel(header);
201  headerLayout->addWidget(iconLabel);
202  iconLabel->setPixmap(KIconLoader::global()->loadMimeTypeIcon(mimeType->iconName(), KIconLoader::Desktop));
203  iconLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
204 
205  KVBox *headerRight = new KVBox(header);
206  headerLayout->addWidget(headerRight);
207  new QLabel(QString(QLatin1String( "<qt><b>%1</b></qt>" ))
208  .arg(fileUrl.fileName()), headerRight
209  );
210  new QLabel(mimeType->comment(), headerRight);
211 
212  header->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
213 
214  m_part = KMimeTypeTrader::self()->createPartInstanceFromQuery<KParts::ReadOnlyPart>(mimeType->name(),
215  m_widget,
216  this);
217 
218  if (!m_part.data()) {
219  return false;
220  }
221 
222  if (m_part.data()->browserExtension()) {
223  connect(m_part.data()->browserExtension(),
224  SIGNAL(openUrlRequestDelayed(KUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)),
225  SLOT(slotOpenUrlRequestDelayed(KUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)));
226  }
227 
228  // #235546
229  // TODO: the user should be warned in a non-intrusive way that some features are going to be disabled
230  // maybe there should be an option controlling this
231  KHTMLPart *khtmlPart = qobject_cast<KHTMLPart*>(m_part.data());
232  if (khtmlPart) {
233  kDebug() << "Disabling javascripts, plugins, java and external references for KHTMLPart";
234  khtmlPart->setJScriptEnabled(false);
235  khtmlPart->setJavaEnabled(false);
236  khtmlPart->setPluginsEnabled(false);
237  khtmlPart->setMetaRefreshEnabled(false);
238  khtmlPart->setOnlyLocalReferences(true);
239  }
240 
241  m_part.data()->openUrl(fileUrl);
242 
243  return true;
244 }
245 
246 void ArkViewer::slotOpenUrlRequestDelayed(const KUrl& url, const KParts::OpenUrlArguments& arguments, const KParts::BrowserArguments& browserArguments)
247 {
248  kDebug() << "Opening URL: " << url;
249 
250  Q_UNUSED(arguments)
251  Q_UNUSED(browserArguments)
252 
253  KRun *runner = new KRun(url, 0, 0, false);
254  runner->setRunExecutables(false);
255 }
256 
257 KService::Ptr ArkViewer::getViewer(const KMimeType::Ptr &mimeType)
258 {
259  // No point in even trying to find anything for application/octet-stream
260  if (mimeType->isDefault()) {
261  return KService::Ptr();
262  }
263 
264  // Try to get a read-only kpart for the internal viewer
265  KService::List offers = KMimeTypeTrader::self()->query(mimeType->name(), QString::fromLatin1("KParts/ReadOnlyPart"));
266 
267  // If we can't find a kpart, try to get an external application
268  if (offers.size() == 0) {
269  offers = KMimeTypeTrader::self()->query(mimeType->name(), QString::fromLatin1("Application"));
270  }
271 
272  if (offers.size() > 0) {
273  return offers.first();
274  } else {
275  return KService::Ptr();
276  }
277 }
278 
279 
280 #include "arkviewer.moc"
QWidget
ArkViewer::view
static void view(const QString &fileName, QWidget *parent=0)
Definition: arkviewer.cpp:89
QFile::remove
bool remove()
QLabel::setPixmap
void setPixmap(const QPixmap &)
QHBoxLayout
QWeakPointer::data
T * data() const
KDialog
arkviewer.h
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
ArkViewer::slotOpenUrlRequestDelayed
void slotOpenUrlRequestDelayed(const KUrl &url, const KParts::OpenUrlArguments &arguments, const KParts::BrowserArguments &browserArguments)
Definition: arkviewer.cpp:246
ArkViewer::sizeHint
virtual QSize sizeHint() const
Definition: arkviewer.cpp:185
QString
Qt::WFlags
typedef WFlags
QWidget::setSizePolicy
void setSizePolicy(QSizePolicy)
QSize
ArkViewer
Definition: arkviewer.h:33
QFrame
QKeyEvent
QLatin1String
ArkViewer::~ArkViewer
virtual ~ArkViewer()
Definition: arkviewer.cpp:56
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
ArkViewer::keyPressEvent
virtual void keyPressEvent(QKeyEvent *event)
Definition: arkviewer.cpp:169
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:36 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