• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

korganizer

  • sources
  • kde-4.12
  • kdepim
  • korganizer
  • themes
themeimporter.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2007 Loïc Corbasson <loic.corbasson@gmail.com>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library 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 GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "themeimporter.h"
23 #include "theme.h"
24 
25 #include <KLocale>
26 
27 using namespace KOrg;
28 
29 ThemeImporter::ThemeImporter() : QXmlStreamReader()
30 {
31 }
32 
33 ThemeImporter::ThemeImporter( QIODevice *device ) : QXmlStreamReader( device )
34 {
35  read( device );
36 }
37 
38 ThemeImporter::~ThemeImporter()
39 {
40  qDeleteAll( mPerViewConfigGroups );
41  mPerViewConfigGroups.clear();
42 }
43 
45 
46 bool ThemeImporter::read( QIODevice *device )
47 {
48  setDevice( device );
49 
50  while ( !atEnd() ) {
51  readNext();
52 
53  if ( isStartElement() ) {
54  if ( name( ) == "korganizer-theme" && attributes().value("version") == "1.0" ) {
55  readThemeXml();
56  } else {
57  raiseError( i18n( "This file is not a KOrganizer theme file." ) );
58  }
59  }
60  }
61 
62  return !error();
63 }
64 
65 void ThemeImporter::readThemeXml()
66 {
67  Q_ASSERT( isStartElement() && name() == "korganizer-theme" );
68 
69  while ( !atEnd() ) {
70  readNext();
71 
72  if ( isEndElement() ) {
73  break;
74  }
75 
76  if ( isStartElement() ) {
77  readElement();
78  }
79  }
80 }
81 
83 
84 void ThemeImporter::readElement( const QString &viewType, const int year,
85  const int month, const int day )
86 {
87  if ( name() == "view" ) {
88  readView( viewType, year, month, day );
89 /***** TODO: Date-dependent themes disabled for now ******
90  else if ( name() == "year" || name() == "month" || name() == "day" )
91  readDate( viewType, year, month, day );
92 */
93 
94  } else if ( name() == "grid" ) {
95  readGrid( viewType, year, month, day );
96  } else if ( name() == "time-labels" ) {
97  readTimeLabels( viewType, year, month, day );
98  } else if ( name() == "calendar-items" ) {
99  readCalendarItems( viewType, year, month, day );
100  } else if ( name() == "marcus-bains-line" ) {
101  readMarcusBainsLine( viewType, year, month, day );
102  } else if ( name() == "holidays" ) {
103  readHolidays( viewType, year, month, day );
104  } else {
105  readUnknownElement();
106  }
107 }
108 
109 void ThemeImporter::readDate( const QString &viewType, const int year,
110  const int month, const int day )
111 {
112  Q_ASSERT( isStartElement() && ( name() == "year" || name() == "month" || name() == "day" ) );
113 
114  int y = year;
115  int m = month;
116  int d = day;
117 
118  if ( name() == "year" ) {
119  y = attributes().value( "value" ).toString().toInt();
120  } else if ( name() == "month" ) {
121  m = attributes().value( "value" ).toString().toInt();
122  } else if ( name() == "day" ) {
123  d = attributes().value( "value" ).toString().toInt();
124  }
125 
126  while ( !atEnd() ) {
127  readNext();
128 
129  if ( isEndElement() ) {
130  break;
131  }
132 
133  if ( isStartElement() ) {
134  if ( name() == "year" || name() == "month" || name() == "day" ) {
135  readDate( viewType, y, m, d );
136  } else {
137  readElement( viewType, y, m, d );
138  }
139  }
140  }
141 }
142 
143 void ThemeImporter::readView( const QString &viewType, const int year,
144  const int month, const int day )
145 {
146  Q_ASSERT( isStartElement() && name() == "view" );
147 
148  QString v = viewType;
149  v = attributes().value( "type" ).toString();
150 
151  while ( !atEnd() ) {
152  readNext();
153 
154  if ( isEndElement() ) {
155  break;
156  }
157 
158  if ( isStartElement() ) {
159  readElement( v, year, month, day );
160  }
161  }
162 }
163 
164 void ThemeImporter::readUnknownElement()
165 {
166  Q_ASSERT( isStartElement() );
167 
168  kWarning() << "Unknown element found at line" << lineNumber()
169  << ", ending at column" << columnNumber()
170  << ":" << name().toString();
171 
172  while ( !atEnd() ) {
173  readNext();
174 
175  if ( isEndElement() ) {
176  break;
177  }
178 
179  if ( isStartElement() ) {
180  readUnknownElement();
181  }
182  }
183 }
184 
186 
187 void ThemeImporter::readCalendarItems( const QString &viewType, const int year,
188  const int month, const int day )
189 {
190  Q_ASSERT( isStartElement() && name() == "calendar-items" );
191 
192  // As the available settings are the same for the various calendar items
193  // types, we use a "stack" to keep in mind where we are in the hierarchy
194  // while having the possibility of using the same methods to read the
195  // settings' tags.
196  QList< QPair<QString, QString> > stack;
197  stack.append( qMakePair( QString(), QString( "CalendarItems" ) ) );
198 
199  while ( !atEnd() ) {
200  readNext();
201 
202  if ( isEndElement() ) {
203  if ( stack.count() > 1 ) {
204  stack.removeLast(); // We are going down one level
205  } else {
206  break;
207  }
208  }
209 
210  if ( isStartElement() ) {
211  /* Item type tags: first level */
212  if ( stack.count() == 1 && name() == "events" ) {
213  stack.append( qMakePair( QString( "events" ),
214  QString( "CalendarItems Events" ) ) );
215  } else if ( stack.count() == 1 && name() == "to-dos" ) {
216  stack.append( qMakePair( QString( "to-dos" ),
217  QString( "CalendarItems ToDos" ) ) );
218  /* Sub-elements of to-dos (second level) */
219  } else if ( stack.count() == 2 && stack.last().first == "to-dos" &&
220  name() == "overdue" ) {
221  stack.append( qMakePair( QString( "to-dos/overdue" ),
222  QString( "CalendarItems ToDos Overdue" ) ) );
223  } else if ( stack.count() == 2 && stack.last().first == "to-dos" &&
224  name() == "due-today" ) {
225  stack.append( qMakePair( QString( "to-dos/due-today" ),
226  QString( "CalendarItems ToDos DueToday" ) ) );
227  /* The sub-elements of these tags allow free text */
228  } else if ( stack.count() == 1 && name() == "categories" ) {
229  stack.append( qMakePair( QString( "categories" ),
230  // When a setting applies to all categories,
231  // it applies to all items.
232  QString( "CalendarItems" ) ) );
233  } else if ( stack.count() == 1 && name() == "resources" ) {
234  stack.append( qMakePair( QString( "resources" ),
235  // When a setting applies to all resources,
236  // it applies to all items.
237  QString( "CalendarItems" ) ) );
238  }
239  /* The said sub-elements */
240  else if ( stack.count() == 2 && stack.last().first == "categories" &&
241  name() == "category" ) {
242  QString n = attributes().value( "name" ).toString();
243  stack.append( qMakePair( QString( "categories/" + n ),
244  QString( "CalendarItems Categories " + n ) ) );
245  } else if ( stack.count() == 2 && stack.last().first == "resources" &&
246  name() == "resource" ) {
247  QString n = attributes().value( "name" ).toString();
248  stack.append( qMakePair( QString( "resources/" + n ),
249  QString( "CalendarItems Resources " + n ) ) );
250  }
251  /* Settings' tags */
252  else if ( name() == "background" ) {
253  setColor( viewType, year, month, day,
254  stack.last().second + " Background Color",
255  attributes().value( "color" ).toString() );
256  setPath( viewType, year, month, day,
257  stack.last().second + " Background Image",
258  attributes().value( "src" ).toString() );
259  readNext();
260  } else if ( name() == "font" ) {
261  setFont( viewType, year, month, day,
262  stack.last().second + " Font",
263  attributes().value( "family" ).toString(),
264  attributes().value( "style-hint" ).toString(),
265  attributes().value( "point-size" ).toString().toInt(),
266  attributes().value( "weight" ).toString().toInt(),
267  attributes().value( "style" ).toString(),
268  attributes().value( "stretch-factor" ).toString().toInt() );
269  readNext();
270  } else if ( name() == "frame" ) {
271  setColor( viewType, year, month, day,
272  stack.last().second + " Frame Color",
273  attributes().value( "color" ).toString() );
274  readNext();
275  } else if ( name() == "icon" ) {
276  setString( viewType, year, month, day,
277  stack.last().second + " Icon",
278  attributes().value( "name" ).toString() );
279  setPath( viewType, year, month, day,
280  stack.last().second + " IconFile",
281  attributes().value( "src" ).toString() );
282  readNext();
283  } else {
284  readUnknownElement();
285  }
286  }
287  }
288 }
289 
290 void ThemeImporter::readGrid( const QString &viewType, const int year,
291  const int month, const int day )
292 {
293  Q_ASSERT( isStartElement() && name() == "grid" );
294 
295  QString cfg = "Grid";
296 
297  while ( !atEnd() ) {
298  readNext();
299 
300  if ( isEndElement() ) {
301  break;
302  }
303 
304  if ( isStartElement() ) {
305  if ( name() == "background" ) {
306  setColor( viewType, year, month, day,
307  cfg + " Background Color",
308  attributes().value( "color" ).toString() );
309  setPath( viewType, year, month, day,
310  cfg + " Background Image",
311  attributes().value( "src" ).toString() );
312  readNext();
313  } else if ( name() == "highlight" ) {
314  setColor( viewType, year, month, day,
315  cfg + " Highlight Color",
316  attributes().value( "color" ).toString() );
317  readNext();
318  } else if ( name() == "work-hours" ) {
319  while ( !atEnd() ) {
320  readNext();
321 
322  if ( isEndElement() ) {
323  break;
324  }
325 
326  if ( isStartElement() ) {
327  if ( name() == "background" ) {
328  setColor( viewType, year, month, day,
329  cfg + " WorkHours Background Color",
330  attributes().value( "color" ).toString() );
331  setPath( viewType, year, month, day,
332  cfg + " WorkHours Background Image",
333  attributes().value( "src" ).toString() );
334  readNext();
335  } else {
336  readUnknownElement();
337  }
338  }
339  }
340  } else {
341  readUnknownElement();
342  }
343  }
344  }
345 }
346 
347 void ThemeImporter::readHolidays( const QString &viewType, const int year,
348  const int month, const int day )
349 {
350  Q_ASSERT( isStartElement() && name() == "holidays" );
351 
352  QString cfg = "Holidays";
353 
354  while ( !atEnd() ) {
355  readNext();
356 
357  if ( isEndElement() ) {
358  break;
359  }
360 
361  if ( isStartElement() ) {
362  if ( name() == "background" ) {
363  setColor( viewType, year, month, day,
364  cfg + " Background Color",
365  attributes().value( "color" ).toString() );
366  setPath( viewType, year, month, day,
367  cfg + " Background Image",
368  attributes().value( "src" ).toString() );
369  readNext();
370  } else {
371  readUnknownElement();
372  }
373  }
374  }
375 }
376 
377 void ThemeImporter::readMarcusBainsLine( const QString &viewType, const int year,
378  const int month, const int day )
379 {
380  Q_ASSERT( isStartElement() && name() == "marcus-bains-line" );
381 
382  QString cfg = "MarcusBainsLine";
383 
384  while ( !atEnd() ) {
385  readNext();
386 
387  if ( isEndElement() ) {
388  break;
389  }
390 
391  if ( isStartElement() ) {
392  if ( name() == "font" ) {
393  setFont( viewType, year, month, day,
394  cfg + " Font",
395  attributes().value( "family" ).toString(),
396  attributes().value( "style-hint" ).toString(),
397  attributes().value( "point-size" ).toString().toInt(),
398  attributes().value( "weight" ).toString().toInt(),
399  attributes().value( "style" ).toString(),
400  attributes().value( "stretch-factor" ).toString().toInt() );
401  readNext();
402  } else if ( name() == "line" ) {
403  setColor( viewType, year, month, day,
404  cfg + " Line Color",
405  attributes().value( "color" ).toString() );
406  readNext();
407  } else {
408  readUnknownElement();
409  }
410  }
411  }
412 }
413 
414 void ThemeImporter::readTimeLabels( const QString &viewType, const int year,
415  const int month, const int day )
416 {
417  Q_ASSERT( isStartElement() && name() == "time-labels" );
418 
419  QString cfg = "TimeLabels";
420 
421  while ( !atEnd() ) {
422  readNext();
423 
424  if ( isEndElement() ) {
425  break;
426  }
427 
428  if ( isStartElement() ) {
429  if ( name() == "font" ) {
430  setFont( viewType, year, month, day,
431  cfg + " Font",
432  attributes().value( "family" ).toString(),
433  attributes().value( "style-hint" ).toString(),
434  attributes().value( "point-size" ).toString().toInt(),
435  attributes().value( "weight" ).toString().toInt(),
436  attributes().value( "style" ).toString(),
437  attributes().value( "stretch-factor" ).toString().toInt() );
438  readNext();
439  } else {
440  readUnknownElement();
441  }
442  }
443  }
444 }
445 
447 
448 void ThemeImporter::setColor( const QString &viewType, const int year,
449  const int month, const int day,
450  const QString &key, const QString &value )
451 {
452  QString htmlColor = value.toUpper();
453  if ( ( htmlColor.count( QRegExp( "^#[0-9A-F]{6}$" ) ) == 1 ) ||
454  ( htmlColor.count( QRegExp( "^#[0-9A-F]{8}$" ) ) == 1 ) ) {
455  // #RRGGBB or #AARRGGBB, for consistency with Qt
456  int r = htmlColor.mid( 1, 2 ).toInt( 0, 16 );
457  int g = htmlColor.mid( 3, 2 ).toInt( 0, 16 );
458  int b = htmlColor.mid( 5, 2 ).toInt( 0, 16 );
459  int a = 255;
460  if ( htmlColor.length() == 1+8 ) {
461  a = r;
462  r = g;
463  g = b;
464  b = htmlColor.mid( 7, 2 ).toInt( 0, 16 );
465  }
466  QColor color( r, g, b, a );
467 
468  foreach ( const QString &v, Theme::themableViews( viewType ) ) {
469  if ( year == 0 && month == 0 && day == 0 ) {
470  configGroup( v )->writeEntry( v + key, color );
471  } else {
472  // TODO: implement this when date-dependent themes will be enabled
473  kWarning() << "feature not yet implemented";
474  kWarning() << "THEORICAL setting:" << year << "-" << month << "-" << day
475  << ":" << v << ":" << key << ":" << value;
476  }
477  }
478  }
479 }
480 
481 void ThemeImporter::setFont( const QString &viewType, const int year,
482  const int month, const int day,
483  const QString &key, const QString &family,
484  const QString &styleHint, const int pointSize,
485  const int weight, const QString &style,
486  const int stretchFactor )
487 {
488  QFont f( family, pointSize, weight );
489 
490  QFont::StyleHint sh = QFont::AnyStyle;
491  if ( styleHint == "AnyStyle" ) {
492  sh = QFont::AnyStyle;
493  } else if ( styleHint == "SansSerif" ) {
494  sh = QFont::SansSerif;
495  } else if ( styleHint == "Helvetica" ) {
496  sh = QFont::Helvetica;
497  } else if ( styleHint == "Serif" ) {
498  sh = QFont::Serif;
499  } else if ( styleHint == "Times" ) {
500  sh = QFont::Times;
501  } else if ( styleHint == "TypeWriter" ) {
502  sh = QFont::TypeWriter;
503  } else if ( styleHint == "Courier" ) {
504  sh = QFont::Courier;
505  } else if ( styleHint == "OldEnglish" ) {
506  sh = QFont::OldEnglish;
507  } else if ( styleHint == "Decorative" ) {
508  sh = QFont::Decorative;
509  } else if ( styleHint == "System" ) {
510  sh = QFont::System;
511  }
512  f.setStyleHint( sh );
513  QFont::Style s = QFont::StyleNormal;
514  if ( style == "Normal" ) {
515  s = QFont::StyleNormal;
516  } else if ( style == "Italic" ) {
517  s = QFont::StyleItalic;
518  } else if ( style == "Oblique" ) {
519  s = QFont::StyleOblique;
520  }
521  f.setStyle( s );
522  int sf = ( stretchFactor < 1 ? 100 : stretchFactor );
523  f.setStretch( sf );
524 
525  foreach ( const QString &v, Theme::themableViews( viewType ) ) {
526  if ( year == 0 && month == 0 && day == 0 ) {
527  configGroup( v )->writeEntry( v + key, f );
528  } else {
529  // TODO: implement this when date-dependent themes will be enabled
530  kWarning() << "feature not yet implemented";
531  kWarning() << "THEORICAL setting:" << year << "-" << month << "-" << day
532  << ":" << v << ":" << key << ":" << family << "\t"
533  << styleHint << "\t" << pointSize << "\t" << weight << "\t"
534  << style << "\t" << sf;
535  }
536  }
537 }
538 
539 void ThemeImporter::setPath( const QString &viewType, const int year,
540  const int month, const int day,
541  const QString &key, const QString &value )
542 {
543  if ( ! value.isEmpty() ) {
544  foreach ( const QString &v, Theme::themableViews( viewType ) ) {
545  if ( year == 0 && month == 0 && day == 0 ) {
546  configGroup( v )->writePathEntry( v + key, value );
547  } else {
548  // TODO: implement this when date-dependent themes will be enabled
549  kWarning() << "feature not yet implemented";
550  kWarning() << "THEORICAL setting:" << year << "-" << month << "-" << day
551  << ":" << v << ":" << key << ":" << value;
552  }
553  }
554  }
555 }
556 
557 void ThemeImporter::setString( const QString &viewType, const int year,
558  const int month, const int day,
559  const QString &key, const QString &value )
560 {
561  if ( ! value.isEmpty() ) {
562  foreach ( const QString &v, Theme::themableViews( viewType ) ) {
563  if ( year == 0 && month == 0 && day == 0 ) {
564  configGroup( v )->writeEntry( v + key, value );
565  } else {
566  // TODO: implement this when date-dependent themes will be enabled
567  kWarning() << "feature not yet implemented";
568  kWarning() << "THEORICAL setting:" << year << "-" << month << "-" << day
569  << ":" << v << ":" << key << ":" << value;
570  }
571  }
572  }
573 }
574 
576 
577 KConfigGroup *ThemeImporter::configGroup( const QString &viewType )
578 {
579  QMap<QString, KConfigGroup*>::ConstIterator it;
580  KConfigGroup *g;
581  it = mPerViewConfigGroups.constFind( viewType );
582  if ( it == mPerViewConfigGroups.constEnd() ) {
583  g = registerPerViewConfigGroup( createPerViewConfigGroup( viewType ), viewType );
584  } else {
585  g = *it;
586  }
587  return g;
588 }
589 
590 QList<KConfigGroup*> ThemeImporter::perViewConfigGroups()
591 {
592  QList<KConfigGroup*> l;
593  foreach ( const QString &v, Theme::themableViews() ) {
594  l.append( configGroup( v ) );
595  }
596  return l;
597 }
598 
599 KConfigGroup *ThemeImporter::registerPerViewConfigGroup( KConfigGroup *g, const QString &viewType )
600 {
601  mPerViewConfigGroups.insert( viewType, g );
602  return g;
603 }
604 
605 KConfigGroup *ThemeImporter::createPerViewConfigGroup( const QString &viewType ) const
606 {
607  return new KConfigGroup( KSharedConfig::openConfig(), "Theme/" + viewType + " view" );
608 }
KOrg::ThemeImporter::ThemeImporter
ThemeImporter()
Definition: themeimporter.cpp:29
KOrg::Theme::themableViews
static const QStringList themableViews(const QString &viewType=QString())
Return all themable views corresponding to the viewType.
Definition: theme.cpp:159
KOrg::ThemeImporter::read
bool read(QIODevice *device)
Read the XML stream from a device.
Definition: themeimporter.cpp:46
theme.h
KOrg::ThemeImporter::~ThemeImporter
~ThemeImporter()
Definition: themeimporter.cpp:38
themeimporter.h
QXmlStreamReader
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

korganizer

Skip menu "korganizer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

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