2 Copyright (c) 2008 Bertjan Broeksema <b.broeksema@kdemail.net>
3 Copyright (c) 2008 Volker Krause <vkrause@kde.org>
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "singlefileresourcebase.h"
23 #include <akonadi/changerecorder.h>
24 #include <akonadi/entitydisplayattribute.h>
25 #include <akonadi/itemfetchscope.h>
28 #include <kio/jobuidelegate.h>
32 #include <KStandardDirs>
34 #include <QtCore/QDir>
36 using namespace Akonadi
;
38 SingleFileResourceBase::SingleFileResourceBase( const QString
& id
)
39 : ResourceBase( id
), mDownloadJob( 0 ), mUploadJob( 0 )
41 connect( &mDirtyTimer
, SIGNAL( timeout() ), SLOT( writeFile() ) );
42 mDirtyTimer
.setSingleShot( true );
44 connect( this, SIGNAL( reloadConfiguration() ), SLOT( reloadFile() ) );
45 QTimer::singleShot( 0, this, SLOT( readFile() ) );
47 changeRecorder()->itemFetchScope().fetchFullPayload();
48 changeRecorder()->fetchCollection( true );
50 connect( KDirWatch::self(), SIGNAL( dirty( QString
) ), SLOT( fileChanged( QString
) ) );
51 connect( KDirWatch::self(), SIGNAL( created( QString
) ), SLOT( fileChanged( QString
) ) );
54 QString
SingleFileResourceBase::cacheFile() const
56 return KStandardDirs::locateLocal( "cache", "akonadi/" + identifier() );
59 void SingleFileResourceBase::setSupportedMimetypes( const QStringList
& mimeTypes
, const QString
&icon
)
61 mSupportedMimetypes
= mimeTypes
;
62 mCollectionIcon
= icon
;
65 void SingleFileResourceBase::collectionChanged( const Akonadi::Collection
& collection
)
67 QString newName
= collection
.name();
68 if ( collection
.hasAttribute
<EntityDisplayAttribute
>() ) {
69 EntityDisplayAttribute
*attr
= collection
.attribute
<EntityDisplayAttribute
>();
70 if ( !attr
->displayName().isEmpty() )
71 newName
= attr
->displayName();
74 if ( newName
!= name() )
77 changeCommitted( collection
);
80 void SingleFileResourceBase::reloadFile()
82 // Update the network setting.
83 setNeedsNetwork( !mCurrentUrl
.isEmpty() && !mCurrentUrl
.isLocalFile() );
85 // if we have something loaded already, make sure we write that back in case
86 // the settings changed
87 if ( !mCurrentUrl
.isEmpty() )
93 void SingleFileResourceBase::handleProgress( KJob
*, unsigned long pct
)
98 void SingleFileResourceBase::fileChanged( const QString
& fileName
)
100 if ( fileName
!= mCurrentUrl
.path() )
104 if ( mDirtyTimer
.isActive() && !mCurrentUrl
.isEmpty() ) {
105 const KUrl prevUrl
= mCurrentUrl
;
107 QString lostFoundFileName
;
109 lostFoundFileName
= KStandardDirs::locateLocal( "data", identifier() + QDir::separator()
110 + prevUrl
.fileName() + "-" + QString::number( ++i
) );
111 } while ( KStandardDirs::exists( lostFoundFileName
) );
112 mCurrentUrl
= KUrl( lostFoundFileName
);
114 emit
warning( i18n( "The file '%1' was changed on disk while there were still pending changes in Akonadi. "
115 "To avoid data loss, a backup of the internal changes has been created at '%2'.",
116 prevUrl
.prettyUrl(), mCurrentUrl
.prettyUrl() ) );
117 mCurrentUrl
= prevUrl
;
124 void SingleFileResourceBase::slotDownloadJobResult( KJob
*job
)
126 if ( job
->error() && job
->error() != KIO::ERR_DOES_NOT_EXIST
) {
127 emit
status( Broken
, i18n( "Could not load file '%1'.", mCurrentUrl
.prettyUrl() ) );
129 readFromFile( KUrl( cacheFile() ).toLocalFile() );
135 emit
status( Idle
, i18nc( "@info:status", "Ready" ) );
139 void SingleFileResourceBase::slotUploadJobResult( KJob
*job
)
141 if ( job
->error() ) {
142 emit
status( Broken
, i18n( "Could not save file '%1'.", mCurrentUrl
.prettyUrl() ) );
148 emit
status( Idle
, i18nc( "@info:status", "Ready" ) );
151 #include "singlefileresourcebase.moc"