25 #include <KMimeTypeTrader>
30 #include <KIconLoader>
32 #include <KMessageBox>
33 #include <KProgressDialog>
34 #include <KPushButton>
36 #include <KIO/NetAccess>
37 #include <khtml_part.h>
39 #include <QHBoxLayout>
48 m_widget =
new KVBox(
this);
49 m_widget->layout()->setSpacing(10);
51 setMainWidget(m_widget);
53 connect(
this, SIGNAL(finished()), SLOT(dialogClosed()));
60 void ArkViewer::dialogClosed()
62 KConfigGroup conf = KGlobal::config()->group(
"Viewer");
66 KProgressDialog progressDialog
67 (
this, i18n(
"Closing preview"),
68 i18n(
"Please wait while the preview is being closed..."));
70 progressDialog.setMinimumDuration(500);
71 progressDialog.setModal(
true);
72 progressDialog.setAllowCancel(
false);
73 progressDialog.progressBar()->setRange(0, 0);
79 const QString previewedFilePath(m_part.
data()->url().pathOrUrl());
81 m_part.
data()->closeUrl();
83 if (!previewedFilePath.isEmpty()) {
91 KMimeType::Ptr mimeType = KMimeType::findByPath(fileName);
92 kDebug() <<
"MIME type" << mimeType->name();
93 KService::Ptr viewer = ArkViewer::getViewer(mimeType);
95 const bool needsExternalViewer = (!viewer.isNull() &&
96 !viewer->hasServiceType(
QLatin1String(
"KParts/ReadOnlyPart")));
97 if (needsExternalViewer) {
102 const KUrl::List fileUrlList = KUrl(fileName);
105 KRun::run(*viewer, fileUrlList, parent,
true);
109 bool viewInInternalViewer =
true;
110 if (viewer.isNull()) {
115 if (!mimeType->isDefault()) {
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(),
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(),
137 KMessageBox::Dangerous);
140 if (response == KMessageBox::Cancel) {
141 viewInInternalViewer =
false;
148 if (viewInInternalViewer) {
150 if (internalViewer->viewInInternalViewer(fileName, mimeType)) {
151 internalViewer->show();
158 KMessageBox::sorry(parent, i18n(
"The internal viewer cannot preview this file."));
159 delete internalViewer;
171 KPushButton *defButton = button(defaultButton());
175 if ((defButton) && (defButton->hasFocus())) {
176 KDialog::keyPressEvent(event);
187 return QSize(560, 400);
190 bool ArkViewer::viewInInternalViewer(
const QString& fileName,
const KMimeType::Ptr& mimeType)
192 const KUrl fileUrl(fileName);
194 setCaption(fileUrl.fileName());
195 restoreDialogSize(KGlobal::config()->group(
"Viewer"));
202 iconLabel->
setPixmap(KIconLoader::global()->loadMimeTypeIcon(mimeType->iconName(), KIconLoader::Desktop));
203 iconLabel->
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
205 KVBox *headerRight =
new KVBox(header);
208 .arg(fileUrl.fileName()), headerRight
210 new QLabel(mimeType->comment(), headerRight);
212 header->
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum);
214 m_part = KMimeTypeTrader::self()->createPartInstanceFromQuery<KParts::ReadOnlyPart>(mimeType->name(),
218 if (!m_part.
data()) {
222 if (m_part.
data()->browserExtension()) {
223 connect(m_part.
data()->browserExtension(),
224 SIGNAL(openUrlRequestDelayed(KUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)),
231 KHTMLPart *khtmlPart = qobject_cast<KHTMLPart*>(m_part.
data());
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);
241 m_part.
data()->openUrl(fileUrl);
248 kDebug() <<
"Opening URL: " << url;
251 Q_UNUSED(browserArguments)
253 KRun *runner =
new KRun(url, 0, 0,
false);
254 runner->setRunExecutables(
false);
257 KService::Ptr ArkViewer::getViewer(
const KMimeType::Ptr &mimeType)
260 if (mimeType->isDefault()) {
261 return KService::Ptr();
265 KService::List offers = KMimeTypeTrader::self()->query(mimeType->name(),
QString::fromLatin1(
"KParts/ReadOnlyPart"));
268 if (offers.size() == 0) {
269 offers = KMimeTypeTrader::self()->query(mimeType->name(),
QString::fromLatin1(
"Application"));
272 if (offers.size() > 0) {
273 return offers.first();
275 return KService::Ptr();
280 #include "arkviewer.moc"
static void view(const QString &fileName, QWidget *parent=0)
void setPixmap(const QPixmap &)
void slotOpenUrlRequestDelayed(const KUrl &url, const KParts::OpenUrlArguments &arguments, const KParts::BrowserArguments &browserArguments)
virtual QSize sizeHint() const
QString fromLatin1(const char *str, int size)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual void keyPressEvent(QKeyEvent *event)
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const