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

akregator

  • sources
  • kde-4.12
  • kdepim
  • akregator
  • src
loadfeedlistcommand.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Akregator.
3 
4  Copyright (C) 2008 Frank Osterfeld <osterfeld@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program 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
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of Qt, and distribute the resulting executable,
22  without including the source code for Qt in the source distribution.
23 */
24 
25 #include "loadfeedlistcommand.h"
26 
27 #include "feedlist.h"
28 #include "storage.h"
29 
30 #include <KLocale>
31 #include <KMessageBox>
32 #include <KRandom>
33 
34 #include <QDateTime>
35 #include <QDomDocument>
36 #include <QFile>
37 #include <QPointer>
38 #include <QString>
39 #include <QTimer>
40 
41 #include <cassert>
42 
43 using namespace boost;
44 using namespace Akregator;
45 using namespace Akregator::Backend;
46 
47 class LoadFeedListCommand::Private {
48  LoadFeedListCommand* const q;
49 public:
50  explicit Private( LoadFeedListCommand* qq ) : q( qq ), storage( 0 ) {}
51  void handleDocument( const QDomDocument& doc );
52  QString createBackup( const QString& path, bool* ok );
53  void emitResult( const shared_ptr<FeedList>& list );
54  void doLoad();
55 
56  QString fileName;
57  QDomDocument defaultFeedList;
58  Storage* storage;
59 };
60 
61 void LoadFeedListCommand::Private::emitResult( const shared_ptr<FeedList>& list ) {
62  emit q->result( list );
63  q->done();
64 }
65 
66 void LoadFeedListCommand::Private::handleDocument( const QDomDocument& doc ) {
67  shared_ptr<FeedList> feedList( new FeedList( storage ) );
68  if ( !feedList->readFromOpml( doc ) ) {
69  bool backupCreated;
70  const QString backupFile = createBackup( fileName, &backupCreated );
71  const QString msg =
72  backupCreated ?
73  i18n("<qt>The standard feed list is corrupted (invalid OPML). "
74  "A backup was created:<p><b>%1</b></p></qt>", backupFile ) :
75  i18n("<qt>The standard feed list is corrupted (invalid OPML). "
76  "Could not create a backup.</qt>" );
77 
78  QPointer<QObject> that( q );
79  KMessageBox::error( q->parentWidget(), msg, i18n("OPML Parsing Error") );
80  if ( !that )
81  return;
82  feedList.reset();
83  }
84  emitResult( feedList );
85 }
86 
87 QString LoadFeedListCommand::Private::createBackup( const QString& path, bool* ok ) {
88  const QString backup = path
89  + QLatin1String("-backup.")
90  + QString::number(QDateTime::currentDateTime().toTime_t());
91 
92  const bool copied = QFile::copy( path, backup );
93  if ( ok )
94  *ok = copied;
95  return backup;
96 }
97 
98 LoadFeedListCommand::LoadFeedListCommand( QObject* parent ) : Command( parent ), d( new Private( this ) ) {
99 }
100 
101 LoadFeedListCommand::~LoadFeedListCommand() {
102  delete d;
103 }
104 
105 void LoadFeedListCommand::setFileName( const QString& fileName ) {
106  d->fileName = fileName;
107 }
108 void LoadFeedListCommand::setDefaultFeedList( const QDomDocument& doc ) {
109  d->defaultFeedList = doc;
110 }
111 
112 void LoadFeedListCommand::setStorage( Backend::Storage* s ) {
113  d->storage = s;
114 }
115 
116 void LoadFeedListCommand::doStart() {
117  QTimer::singleShot( KRandom::random() % 400, this, SLOT(doLoad()) );
118 }
119 
120 void LoadFeedListCommand::doAbort() {
121 
122 }
123 
124 void LoadFeedListCommand::Private::doLoad() {
125  assert( storage );
126  assert( !fileName.isNull() );
127  emit q->progress( 0, i18n("Opening Feed List...") );
128 
129 
130  QString str;
131 
132  const QString listBackup = storage->restoreFeedList();
133 
134  QDomDocument doc;
135 
136  if ( !QFile::exists( fileName ) ) {
137  handleDocument( defaultFeedList );
138  return;
139  }
140 
141  QFile file( fileName );
142 
143  if ( !file.open( QIODevice::ReadOnly ) ) {
144  QPointer<QObject> that( q );
145  KMessageBox::error(
146  q->parentWidget(),
147  i18n( "<qt>Could not open feed list (%1) for reading.</qt>", file.fileName() ),
148  i18n( "Read Error" ) );
149  if ( that )
150  handleDocument( defaultFeedList );
151  return;
152  }
153 
154  QString errMsg;
155  int errLine = 0;
156  int errCol = 0;
157  if ( !doc.setContent( &file, true, &errMsg, &errLine, &errCol ) ) {
158  bool backupCreated = false;
159  const QString backupFile = createBackup( fileName, &backupCreated );
160  const QString title = i18nc( "error message window caption", "XML Parsing Error" );
161  const QString details =
162  i18n( "<qt><p>XML parsing error in line <numid>%1</numid>, "
163  "column <numid>%2</numid> of %3:</p><p>%4</p></qt>",
164  errLine,
165  errCol,
166  fileName,
167  errMsg );
168  const QString msg =
169  backupCreated ?
170  i18n( "<qt>The standard feed list is corrupted (invalid XML). "
171  "A backup was created:<p><b>%1</b></p></qt>", backupFile ) :
172  i18n( "<qt>The standard feed list is corrupted (invalid XML). "
173  "Could not create a backup.</qt>" );
174 
175  QPointer<QObject> that( q );
176 
177  KMessageBox::detailedError( q->parentWidget(), msg, details, title );
178 
179  if ( that )
180  handleDocument( defaultFeedList );
181  return;
182  }
183 
184  handleDocument( doc );
185 }
186 
187 #include "loadfeedlistcommand.moc"
Akregator::LoadFeedListCommand::setDefaultFeedList
void setDefaultFeedList(const QDomDocument &doc)
Definition: loadfeedlistcommand.cpp:108
feedlist.h
Akregator::LoadFeedListCommand
Definition: loadfeedlistcommand.h:42
Akregator::LoadFeedListCommand::setStorage
void setStorage(Backend::Storage *storage)
Definition: loadfeedlistcommand.cpp:112
Akregator::Command
Definition: command.h:36
Akregator::Backend::Storage
Storage is the main interface to the article archive.
Definition: storage.h:43
QObject
Akregator::FeedList
The model of a feed tree, represents an OPML document.
Definition: feedlist.h:77
storage.h
loadfeedlistcommand.h
Akregator::LoadFeedListCommand::setFileName
void setFileName(const QString &fileName)
Definition: loadfeedlistcommand.cpp:105
Akregator::LoadFeedListCommand::~LoadFeedListCommand
~LoadFeedListCommand()
Definition: loadfeedlistcommand.cpp:101
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akregator

Skip menu "akregator"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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