This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
forked from jeremysanders/okularplugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpartwin.cpp
285 lines (222 loc) · 7.6 KB
/
partwin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// okular firefox plugin
// Copyright (C) 2010 Jeremy Sanders
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA
#include <QtGui>
#include <kservice.h>
#include <kmessagebox.h>
#include <kactioncollection.h>
#include <KDebug>
#include <KMenuBar>
#include <KToolBar>
#include <QToolButton>
#include "partwin.h"
#include <KStandardDirs>
#include <KXMLGUIFactory>
#define PART_DEBUG() kDebugDevNull()
//#define PART_DEBUG() kWarning()
PartWin::PartWin(QWidget *parent)
: KParts::MainWindow(), m_progressBarInited(false), m_guiInitialized(false)
{
PART_DEBUG() << " me == " << this;
QString dataDir = KStandardDirs::locate("data", "okularplugin/");
setXMLFile("okularplugin/okularpluginui.rc", false);
if (! KStandardDirs::exists(dataDir)) {
KStandardDirs::makeDir(dataDir);
}
// Set the full path to the "local" xml file, the one used for saving toolbar and shortcut changes
setLocalXMLFile("okularplugin/okularpluginui.rc");
// if( parent != 0 )
// setParent(parent);
setFocusPolicy(Qt::StrongFocus);
//QApplication::setActiveWindow(this);
// query the .desktop file to load the requested Part
KService::Ptr service =
KService::serviceByDesktopPath("okular_part.desktop");
if (service) {
PART_DEBUG() << " createInstance";
m_part = service->createInstance<KParts::ReadOnlyPart>(this);
if (!m_part)
return;
if (m_part) {
// replace ui definition
m_part->replaceXMLFile(dataDir + "okularplugin_okularui.rc",
"okularplugin/okularplugin_okularui.rc",
false);
m_progressWidget = new ProgressWidget(this->parentWidget());
setCentralWidget(m_progressWidget);
m_progressWidget->setAttribute(Qt::WA_DeleteOnClose);
m_guiInitialized = true;
}
} else {
// couldn't load anything up
KMessageBox::error(this, "service okular_part.desktop not found");
qApp->quit();
return;
}
}
void PartWin::setupPart() {
// make part window the main widget
setCentralWidget(m_part->widget());
setupActions();
setupGUI(ToolBar | Keys | Save);
toolBar("okularToolBar")->setToolButtonStyle(Qt::ToolButtonIconOnly);
// integrate the part's GUI with the shell's
createGUI(m_part);
toolBar("okularToolBar")->insertAction(toolBar("okularToolBar")->actions().front(), this->actionCollection()->action("file_print"));
menuBar()->clear();
toolBar("okularToolBar")->show();
}
void PartWin::setupActions() {
// make a print action, as we don't get this by
// default in the okular kpart
m_printAction = KStandardAction::print(m_part, SLOT( slotPrint() ), actionCollection());
}
PartWin::~PartWin()
{
this->guiFactory()->removeClient(m_part);
this->guiFactory()->removeClient(this);
delete m_part;
QDir d;
for( QList<QString>::const_iterator i = m_filesToDelete.begin(); i != m_filesToDelete.end(); ++i ) {
d.remove(*i);
}
}
/**
* This method is called from qtbrowserplugin when the download
* is already finished.
*
*/
bool PartWin::readData(QIODevice *source, const QString &format)
{
// NPRES_NETWORK_ERR:
if (source->errorString() == "Network error during download.") {
// this error message is also set when user aborts download before it finished.
m_progressWidget->resetProgressBar();
m_progressWidget->setErrorMessage("Error occurred during download. Reload tab to retry.");
return false;
}
// NPRES_USER_BREAK:
if (source->errorString() == "User cancelled operation.") {
m_progressWidget->resetProgressBar();
m_progressWidget->setErrorMessage("Error occurred during download. Reload tab to retry.");
return false;
}
// Download finished
m_progressWidget->setValue(m_progressWidget->getMaximum());
QString filetype;
QFileInfo fileInfo(QUrl(sourceUrl).path());
QString fileName = QUrl::fromPercentEncoding(fileInfo.fileName().toUtf8());
QString extension = fileInfo.suffix();
if( format == "application/postscript" )
filetype = ".ps";
else if ( format == "application/x-dvi" )
filetype = ".dvi";
else if ( format == "image/vnd.djvu" )
filetype = ".djvu";
else if ( format == "image/x.djvu" )
filetype = ".djvu";
else if ( format == "image/x-djvu" )
filetype = ".djvu";
else if ( format == "application/x-chm" )
filetype = ".chm";
else if ( format == "application/epub+zip" )
filetype = ".epub";
else if ( format == "image/tiff" )
filetype = ".tiff";
else if ( format == "image/tiff-fx" )
filetype = ".tiff";
else if ( format == "application/vnd.ms-xpsdocument" )
filetype = ".xps";
else if ( format == "image/g3fax" )
filetype = ".fax";
else if ( format == "text/fb2+xml" )
filetype = ".fb2";
else if ( format == "application/vnd.palm" )
filetype = ".pdb";
else if ( format == "application/x-cbr" ) {
if(extension == "cba")
filetype = ".cba";
else if(extension == "cbt")
filetype = ".cbt";
else if(extension == "cbz")
filetype = ".cbz";
else if(extension == "cb7")
filetype = ".cb7";
else
filetype = ".cbr";
} else {
filetype= ".pdf";
}
if (fileName.endsWith(extension, Qt::CaseInsensitive)) {
// foo.pdf -> foo - pdf - .
fileName = fileName.left(fileName.length() - extension.length() - ((extension.length() > 0) ? 1 : 0));
}
QTemporaryFile tmpFile("/tmp/" + fileName + "_XXXXXX" + filetype);
tmpFile.setAutoRemove(false);
if (!source->open(QIODevice::ReadOnly))
return false;
if(tmpFile.open()) {
while( ! source->atEnd() ) {
QByteArray data = source->read(1024 * 1024 * 1024);
tmpFile.write(data);
}
tmpFile.flush();
}
m_filesToDelete.push_back(tmpFile.fileName());
this->setupPart();
QString url = QString("file://") + tmpFile.fileName();
m_part->openUrl(url);
return true;
}
void PartWin::readProgress(int lenRead, int size) {
if (!m_progressBarInited) {
m_progressWidget->setMaximum(size);
m_progressWidget->setMinimum(0);
m_progressBarInited = true;
}
m_progressWidget->setValue(m_progressWidget->getValue() + lenRead);
}
void PartWin::setDataSourceUrl(const QString &url)
{
sourceUrl = url;
QFileInfo fileInfo(QUrl(sourceUrl).path());
QString fileName = fileInfo.fileName();
m_progressWidget->setFileName(fileName);
}
QString PartWin::dataSourceUrl() const
{
return sourceUrl;
}
void PartWin::transferComplete(const QString &url, int id, Reason r)
{
lastConfId = id;
lastConfUrl = url;
lastConfReason = r;
}
void PartWin::enterEvent(QEvent *event)
{
// this is required because firefox stops sending keyboard
// events to the plugin after opening windows (e.g. download dialog)
// setting the active window brings the events back
if ( QApplication::activeWindow() == NULL ) {
QApplication::setActiveWindow(this);
}
QWidget::enterEvent(event);
}
// required for signals and slots - generated by Qt
#include "partwin.moc"
QTNPFACTORY_BEGIN("Okular plugin",
"Okular plugin using KParts");
QTNPCLASS(PartWin)
QTNPFACTORY_END()