/
ImageWindow.cpp
154 lines (126 loc) · 4.71 KB
/
ImageWindow.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
#include <QDebug>
#include "ImageWindow.h"
#include "BrowseWidget.h"
#include <KFilterDev>
#include <QApplication>
#include <KMessageBox>
#include <QDomDocument>
#include <QDomElement>
#include <QDomAttr>
#include <QFileInfo>
#include <QByteArray>
#include <QTextStream>
#include <QFileDialog>
#include <KStandardDirs>
#include <KUrl>
#include <KJob>
#include <kio/jobuidelegate.h>
#include <kio/scheduler.h>
#include <kio/jobclasses.h>
#include <kio/filejob.h>
#define CHUNK 16384
ImageWindow::ImageWindow(QApplication* app, QWidget *parent) : KDialog(parent)
{
m_app = app;
m_browser = new BrowseWidget(this);
setCaption("decKreator");
setButtons(KDialog::Apply | KDialog::Cancel);
enableButton(KDialog::Apply, false);
this->setMainWidget(m_browser);
connect(this, SIGNAL(applyClicked()), this, SLOT(applyButtonPressed()));
connect(this, SIGNAL(cancelClicked()), this, SLOT(cancelButtonPressed()));
connect(m_browser, SIGNAL(nameChanged()), this, SLOT(enableApply()));
}
void ImageWindow::enableApply(){
enableButton(KDialog::Apply, m_browser->validInputs());
}
void ImageWindow::applyButtonPressed()
{
QString template_path;
template_path = KStandardDirs::locate( "data" , "deckreator/custom-deck.svg");
/* QFile file(template_path);
if (!file.open(QIODevice::ReadOnly)){
KMessageBox::error(this, QString("Could not open file custom-deck.svg. Looked in ") + template_path, "ERROR");
return;
}
*/
// KIO::Job * job = KIO::get(KUrl(template_path));
qDebug() << "Template Path: " << template_path;
KIO::Job * job = KIO::open(KUrl(template_path), QIODevice::ReadOnly);
connect(job, SIGNAL( open(KIO::Job *)), this, SLOT(slotOpen(KIO::Job*)));
connect(job, SIGNAL( result(KJob*)), this, SLOT(slotResult(KJob*)));
}
void ImageWindow::slotOpen(KIO::Job* job)
{
KIO::FileJob* fjob = static_cast<KIO::FileJob*>(job);
fjob->read(fjob->size());
connect( job, SIGNAL( data(KIO::Job* , const QByteArray) ),
this, SLOT( slotData( KIO::Job*, const QByteArray& ) ) ); //from api.kde.org
}
void ImageWindow::slotResult(KJob* job)
{
if ( job->error() )
static_cast<KIO::Job*>(job)->ui()->showErrorMessage();
}
void ImageWindow::slotData(KIO::Job* job, const QByteArray& file_contents)
{
QDomDocument doc("Custom Deck");
if (!doc.setContent(file_contents)) {
KMessageBox::error(this, "Could not parse custom-deck.svg file", "ERROR");
return;
}
KUrl url = m_browser->url();
QString path = url.path();
if (path.isEmpty()){
KMessageBox::error(this, "No image path selected.", "ERROR");
return;
}
if (!QFile::exists(path)){
KMessageBox::error(this, path + " is not a valid file path.", "ERROR");
return;
}
QString name = m_browser->name(); //Put the name typed by the user into this varable
QDomNodeList nodes = doc.elementsByTagName("image");
QDomElement back;
back = nodes.at(0).toElement();
back.setAttribute("xlink:href", path);
back.setAttribute("sodipodi:absref", path);
QString deck_path = KStandardDirs::locateLocal( "data" , QString("carddecks/custom-") + name + "/", true);
QFile index_file(deck_path + "index.desktop"); //Create a new index file for the custom deck.
if (!index_file.open(QIODevice::WriteOnly | QFile::Truncate)){
KMessageBox::error(this, QString("Could not open file ") + deck_path + "index.desktop for writing.", "ERROR");
return;
}
QString image_base = QFileInfo(path).fileName();
QTextStream out(&index_file);
out << "[KDE Backdeck]" << endl;
out << "Name=" << name << endl;
out << "Preview=" << image_base << endl;
out << "SVG=custom-deck.svgz" << endl;
out << "Back=" << name << endl;
out << "Author=decKreator Theming Tool" << endl;
out << "Author=kratos_12000@yahoo.com" << endl;
out << "Comment=Theme based on Oxygen Air Theme by Eugene Trounev" << endl;
out << "Comment=Created by decKreator" << endl;
index_file.close();
QFile preview_file(path);
if (!preview_file.open(QIODevice::ReadOnly)){
KMessageBox::error(this, QString("Could not open file ") + path, "ERROR");
return;
}
preview_file.copy(deck_path + image_base);
QString filename = deck_path + "custom-deck.svgz";
KFilterDev* zipfile = static_cast<KFilterDev*>(KFilterDev::deviceForFile(filename, "application/x-gzip"));
if (!zipfile){
KMessageBox::error(this, QString("Could not find compression filter"), "ERROR");
}
zipfile->setOrigFileName(filename.toLatin1());
zipfile->open(QIODevice::WriteOnly);
zipfile->write(doc.toString().toLatin1(), doc.toString().size());
zipfile->close();
KMessageBox::information(this, "Success!", "decKreator has successfully created a custom theme for your KPatience enjoyment.");
}
void ImageWindow::cancelButtonPressed()
{
m_app->quit();
}