~ubuntu-branches/ubuntu/breezy/kdemultimedia/breezy

« back to all changes in this revision

Viewing changes to noatun/modules/htmlexport/htmlexport.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-03-24 04:48:58 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050324044858-8ff88o9jxej6ii3d
Tags: 4:3.4.0-0ubuntu3
Add kubuntu_02_hide_arts_menu_entries.diff to hide artsbuilder and artscontrol k-menu entries

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <klocale.h>
 
2
#include <qregexp.h>
 
3
#include <qtextcodec.h>
 
4
#include <kaction.h>
 
5
#include <noatun/stdaction.h>
 
6
#include "htmlexport.h"
 
7
 
 
8
extern "C"
 
9
{
 
10
        KDE_EXPORT Plugin *create_plugin()
 
11
        {
 
12
                return new HTMLExport();
 
13
        }
 
14
}
 
15
 
 
16
HTMLExport::HTMLExport(): QObject(0, "HTMLExport"), Plugin()
 
17
{
 
18
        NOATUNPLUGINC(HTMLExport);
 
19
 
 
20
        mAction = new KAction(i18n("&Export Playlist..."), "filesaveas", 0,
 
21
                this, SLOT(slotExport()), this, "exportlist");
 
22
        napp->pluginActionMenu()->insert(mAction);
 
23
 
 
24
        new Prefs(this);
 
25
        config = KGlobal::config();
 
26
}
 
27
 
 
28
HTMLExport::~HTMLExport()
 
29
{
 
30
        napp->pluginActionMenu()->remove(mAction);
 
31
}
 
32
 
 
33
void HTMLExport::slotExport()
 
34
{
 
35
        // init readConfig
 
36
        config->setGroup("HTMLExport");
 
37
 
 
38
        // get output target
 
39
        KURL url = KFileDialog::getSaveURL(QString::null,
 
40
                                           "text/html",
 
41
                                           0,
 
42
                                           i18n("Export Playlist"));
 
43
 
 
44
        // write into tempfile
 
45
        KTempFile temp;
 
46
        temp.setAutoDelete(true);
 
47
        QFile file(temp.name());
 
48
        file.open(IO_WriteOnly);
 
49
        QTextStream str(&file);
 
50
        str.setCodec(QTextCodec::codecForName("utf8"));
 
51
 
 
52
        str << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
 
53
        str << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">" << endl;
 
54
        str << "<!-- Generated by Noatun " << NOATUN_MAJOR << "." << NOATUN_MINOR << "." << NOATUN_PATCHLEVEL << " -->" << endl;
 
55
 
 
56
        str << endl;
 
57
 
 
58
        str << "<html>" << endl;
 
59
        str << "<head>" << endl;
 
60
        str << "\t<title>" << i18n("Noatun Playlist") << "</title>" << endl;
 
61
 
 
62
        str << "\t<style type=\"text/css\">" << endl;
 
63
        str << "\t\t<!--" << endl;
 
64
        // Set the heading color
 
65
        str << "\t\th1 { color:#"<< getColorByEntry("headColor")<<"; }" << endl;
 
66
 
 
67
        // Optionally set the background image
 
68
        if (!config->readPathEntry( "bgImgPath" ).stripWhiteSpace().isEmpty()) {
 
69
                // Image
 
70
                str << "\t\tp,li { color:#"<< getColorByEntry("txtColor") << "; }" << endl;
 
71
                str << "\t\tbody { background-image: url(\"" << config->readPathEntry( "bgImgPath" ) << "\"); }" << endl;
 
72
        }
 
73
        else {
 
74
                // Color
 
75
                str << "\t\tp,li,body { color:#"<< getColorByEntry("txtColor");
 
76
                str << "; background-color:#" << getColorByEntry( "bgColor" ) << "; }" << endl;
 
77
        }
 
78
 
 
79
        // Links are text colored, too
 
80
        str << "\t\ta { color:#" << getColorByEntry("txtColor") << "; }" << endl;
 
81
        if (getColorByEntry("hoverColor") != getColorByEntry("txtColor"))
 
82
                str << "\t\ta:hover { color:#"<< getColorByEntry("hoverColor")<<"; }"<< endl;
 
83
 
 
84
        str << "\t\t-->" << endl;
 
85
        str << "\t</style>" << endl;
 
86
 
 
87
        str << "</head>" << endl;
 
88
        str << endl;
 
89
        str << "<body>" << endl;
 
90
        str << "\t<h1>" << i18n("Noatun Playlist") << "</h1>" << endl;
 
91
 
 
92
        // Cache the config settings used in the big loop
 
93
        bool link_entries = config->readBoolEntry( "linkEntries" );
 
94
        bool number_entries = config->readBoolEntry( "numberEntries" );
 
95
 
 
96
        if (number_entries)
 
97
                str << "\t\t<ol>" << endl;
 
98
        else
 
99
                str << "\t\t<p>" << endl;
 
100
 
 
101
 
 
102
        for (PlaylistItem item = napp->playlist()->getFirst();item;item = napp->playlist()->getAfter(item)) {
 
103
                str << "\t\t\t";
 
104
 
 
105
                if (number_entries)
 
106
                        str << "<li>";
 
107
 
 
108
                if (link_entries)
 
109
                        str << "<a href=\"" << htmlEscape(item.file()) << "\">";
 
110
 
 
111
                str << htmlEscape(item.title());
 
112
 
 
113
                if (link_entries)
 
114
                        str << "</a>";
 
115
 
 
116
                if (number_entries)
 
117
                        str << "</li>" << endl;
 
118
                else
 
119
                        str << "<br />" << endl;
 
120
        }
 
121
 
 
122
        if (number_entries)
 
123
                str << "\t\t</ol>" << endl;
 
124
        else
 
125
                str << "\t\t</p>" << endl;
 
126
 
 
127
        str << "\t</body>" << endl;
 
128
        str << "</html>" << endl;
 
129
 
 
130
        file.close();
 
131
        // tempfile -> userdefined file
 
132
        KIO::NetAccess::upload(temp.name(), url, 0);
 
133
}
 
134
 
 
135
QString HTMLExport::htmlEscape(const QString &source) {
 
136
        // Escape characters that need to be escaped
 
137
        QString temp = source;
 
138
 
 
139
        temp.replace( QRegExp("&"), "&amp;" );
 
140
        temp.replace( QRegExp("<"), "&lt;" );
 
141
        temp.replace( QRegExp(">"), "&gt;" );
 
142
 
 
143
        return temp;
 
144
}
 
145
 
 
146
QString HTMLExport::getColorByEntry(QString s)
 
147
{
 
148
   QString res;
 
149
   QString tmp;
 
150
   QColor c;
 
151
 
 
152
   // init readConfig
 
153
 
 
154
   config->setGroup("HTMLExport");
 
155
 
 
156
   c = config->readColorEntry( s );
 
157
   tmp = QString::number( c.red(), 16);
 
158
   if (tmp.length()==1) tmp="0"+tmp;
 
159
   res = tmp;
 
160
 
 
161
   tmp = QString::number( c.green(), 16);
 
162
   if (tmp.length()==1) tmp="0"+tmp;
 
163
   res += tmp;
 
164
 
 
165
   tmp = QString::number( c.blue(), 16);
 
166
   if (tmp.length()==1) tmp="0"+tmp;
 
167
   res += tmp;
 
168
 
 
169
   return res;
 
170
 
 
171
}
 
172
//////////////////////////////////// Settings ////////////////////////////////////
 
173
 
 
174
Prefs::Prefs(QObject *parent)
 
175
        : CModule(i18n("Playlist Export"), i18n("Colors & Settings for HTML Export"), "html", parent)
 
176
{
 
177
 
 
178
        // Init Config
 
179
        KConfig *config = KGlobal::config();
 
180
        config->setGroup("HTMLExport");
 
181
 
 
182
        // Set default values
 
183
        if ( !config->hasKey( "headColor" ) )
 
184
                config->writeEntry( "headColor", QColor( black ) ) ;
 
185
 
 
186
        if ( !config->hasKey( "hoverColor" ) )
 
187
                config->writeEntry( "hoverColor", QColor( black ) );
 
188
 
 
189
        if ( !config->hasKey( "bgColor" ) )
 
190
                config->writeEntry( "bgColor", QColor( white ) ) ;
 
191
 
 
192
        if ( !config->hasKey( "txtColor" ) )
 
193
                config->writeEntry( "txtColor", QColor( black ) );
 
194
 
 
195
        config->sync();
 
196
 
 
197
        // Draw Stuff and insert Settings
 
198
        QVBoxLayout *top = new QVBoxLayout(this, KDialog::marginHint(),
 
199
                                                KDialog::spacingHint() );
 
200
 
 
201
        colorBox = new QGroupBox( i18n( "HTML Color Settings"  ), this, "colorBox" );
 
202
 
 
203
        bgcolorLabel = new QGridLayout( colorBox, 2, 5,
 
204
                                        KDialog::marginHint(), KDialog::spacingHint() );
 
205
 
 
206
        headColorSelect = new KColorButton( colorBox, "headColorSelect" );
 
207
 
 
208
        hoverColorSelect = new KColorButton( colorBox, "hoverColorSelect" );
 
209
 
 
210
        bgColorSelect = new KColorButton( colorBox, "bgColorSelect" );
 
211
 
 
212
        txtColorSelect = new KColorButton( colorBox, "txtColorSelect" );
 
213
 
 
214
        txtColorLabel = new QLabel( colorBox, "txtColorLabel" );
 
215
        txtColorLabel->setText( i18n( "Text:"  ) );
 
216
        txtColorLabel->setAlignment( int( QLabel::AlignVCenter | QLabel::AlignRight ) );
 
217
 
 
218
        bgColorLabel = new QLabel( colorBox, "bgColorLabel" );
 
219
        bgColorLabel->setText( i18n( "Background:"  ) );
 
220
        bgColorLabel->setAlignment( int( QLabel::AlignVCenter | QLabel::AlignRight ) );
 
221
 
 
222
        headColorLabel = new QLabel( colorBox, "headColorLabel" );
 
223
        headColorLabel->setText( i18n( "Heading:"  ) );
 
224
        headColorLabel->setAlignment( int( QLabel::AlignVCenter | QLabel::AlignRight ) );
 
225
 
 
226
        hoverColorLabel = new QLabel( colorBox, "hoverColorLabel" );
 
227
        hoverColorLabel->setText( i18n( "Link hover:" ) );
 
228
        hoverColorLabel->setAlignment( int( QLabel::AlignVCenter | QLabel::AlignRight ) );
 
229
 
 
230
        bgcolorLabel->setRowStretch(0, 1);
 
231
 
 
232
        // Makes the spacing nice
 
233
        bgcolorLabel->setColStretch(1, 2);
 
234
        bgcolorLabel->setColStretch(2, 1);
 
235
        bgcolorLabel->setColStretch(4, 2);
 
236
 
 
237
        bgcolorLabel->addWidget( txtColorLabel, 0, 0 );
 
238
        bgcolorLabel->addWidget( txtColorSelect, 0, 1 );
 
239
        bgcolorLabel->addWidget( headColorLabel, 1, 0 );
 
240
        bgcolorLabel->addWidget( headColorSelect, 1, 1 );
 
241
        bgcolorLabel->addWidget( bgColorLabel, 0, 3 );
 
242
        bgcolorLabel->addWidget( bgColorSelect, 0, 4 );
 
243
        bgcolorLabel->addWidget( hoverColorLabel, 1, 3 );
 
244
        bgcolorLabel->addWidget( hoverColorSelect, 1, 4 );
 
245
 
 
246
 
 
247
        // Set up the Background Image frame
 
248
        bgPicBox = new QHGroupBox( i18n( "Background Image"), this, "bgPicBox" );
 
249
 
 
250
        // Set up the URL requestor
 
251
        bgPicPath = new KURLRequester ( bgPicBox, "bgPicPath" );
 
252
        bgPicPath->setShowLocalProtocol(true);
 
253
 
 
254
        // Set up the URL requestor's file dialog
 
255
        bgPicPath->setMode(KFile::File | KFile::ExistingOnly);
 
256
        bgPicPath->setFilter("image/gif image/jpeg image/gif image/png image/svg+xml image/tiff");
 
257
 
 
258
        linkEntries = new QCheckBox( this, "linkEntries" );
 
259
        linkEntries->setText( i18n( "Hyper&link playlist entries to their URL"  ) );
 
260
        linkEntries->setTristate( false );
 
261
 
 
262
        numberEntries = new QCheckBox( this, "numberEntries" );
 
263
        numberEntries->setText( i18n( "&Number playlist entries"  ) );
 
264
        numberEntries->setTristate( false );
 
265
 
 
266
        top->addWidget( colorBox );
 
267
        top->addWidget( bgPicBox );
 
268
        top->addWidget( linkEntries );
 
269
        top->addWidget( numberEntries );
 
270
 
 
271
        top->addStretch();
 
272
 
 
273
    reopen();
 
274
}
 
275
 
 
276
void Prefs::save()
 
277
{
 
278
        KConfig *config = KGlobal::config();
 
279
 
 
280
        QString bgRealURL = bgPicPath->url();
 
281
 
 
282
        if (bgRealURL[0] == '/')
 
283
                bgRealURL = "file:" + bgRealURL;
 
284
 
 
285
        config->setGroup( "HTMLExport" );
 
286
        config->writeEntry( "bgColor", bgColorSelect->color() );
 
287
        config->writeEntry( "txtColor", txtColorSelect->color() );
 
288
        config->writeEntry( "headColor", headColorSelect->color() );
 
289
        config->writeEntry( "hoverColor", hoverColorSelect->color() );
 
290
        config->writePathEntry( "bgImgPath", bgRealURL );
 
291
        config->writeEntry( "linkEntries", linkEntries->isChecked() );
 
292
        config->writeEntry( "numberEntries", numberEntries->isChecked() );
 
293
        config->sync();
 
294
}
 
295
 
 
296
void Prefs::reopen()
 
297
{
 
298
        KConfig *config = KGlobal::config();
 
299
        headColorSelect->setColor(config->readColorEntry( "headColor" ) );
 
300
        hoverColorSelect->setColor( config->readColorEntry( "hoverColor" ) );
 
301
        bgColorSelect->setColor( config->readColorEntry( "bgColor" ) );
 
302
        txtColorSelect->setColor( config->readColorEntry( "txtColor" ) );
 
303
        bgPicPath->setURL( config->readPathEntry( "bgImgPath" ) );
 
304
        numberEntries->setChecked( config->readBoolEntry( "numberEntries" ) );
 
305
        linkEntries->setChecked( config->readBoolEntry( "linkEntries" ) );
 
306
}
 
307
#include "htmlexport.moc"
 
308