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

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • libs
  • kasten
  • core
  • system
documentsyncmanager.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Kasten Framework, made within the KDE community.
3 
4  Copyright 2007-2009,2011 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "documentsyncmanager.h"
24 
25 // lib
26 #include "abstractoverwritedialog.h"
27 #include "abstractsavediscarddialog.h"
28 #include "jobmanager.h"
29 #include "documentmanager.h"
30 #include <abstractloadjob.h>
31 #include <abstractconnectjob.h>
32 #include <abstractsynctoremotejob.h>
33 #include <abstractsyncwithremotejob.h>
34 #include <abstractsyncfromremotejob.h>
35 #include <abstractmodelsynchronizerfactory.h>
36 #include <abstractdocument.h>
37 // KDE
38 #include <KIO/NetAccess>
39 #include <KFileDialog>
40 #include <KLocale>
41 // Qt
42 #include <QtGui/QApplication>
43 
44 
45 namespace Kasten2
46 {
47 
48 
49 DocumentSyncManager::DocumentSyncManager( DocumentManager* manager )
50  : mManager( manager ),
51  mSynchronizerFactory( 0 ),
52  mSaveDiscardDialog( 0 ),
53  mOverwriteDialog( 0 )
54 {}
55 
56 void DocumentSyncManager::setSaveDiscardDialog( AbstractSaveDiscardDialog* saveDiscardDialog )
57 {
58  mSaveDiscardDialog = saveDiscardDialog;
59 }
60 
61 void DocumentSyncManager::setOverwriteDialog( AbstractOverwriteDialog* overwriteDialog )
62 {
63  mOverwriteDialog = overwriteDialog;
64 }
65 
66 // TODO: make a difference between stream/storage formats and work/live formats?
67 QStringList DocumentSyncManager::supportedRemoteTypes() const
68 {
69  return QStringList( mSynchronizerFactory->supportedRemoteType() );
70 }
71 
72 bool DocumentSyncManager::hasSynchronizerForLocal( const QString& workDocumentType ) const
73 {
74  // TODO: need synchronizerfactory classes to query for this or a local datastructure
75  return ( mSynchronizerFactory->supportedWorkType() == workDocumentType );
76 }
77 
78 KUrl DocumentSyncManager::urlOf( AbstractDocument* document ) const
79 {
80  AbstractModelSynchronizer* synchronizer = document->synchronizer();
81 
82  return synchronizer ? synchronizer->url() : KUrl();
83 }
84 
85 void DocumentSyncManager::setDocumentSynchronizerFactory( AbstractModelSynchronizerFactory* synchronizerFactory )
86 {
87  mSynchronizerFactory = synchronizerFactory;
88 }
89 
90 void DocumentSyncManager::load( const KUrl& url )
91 {
92  foreach( AbstractDocument* document, mManager->documents() )
93  {
94  if( url == urlOf(document) )
95  {
96  // TODO: query if file should be reloaded/synched from disk
97  emit mManager->focusRequested( document );
98  return;
99  }
100  }
101 
102  AbstractModelSynchronizer* synchronizer = mSynchronizerFactory->createSynchronizer();
103  AbstractLoadJob* loadJob = synchronizer->startLoad( url );
104  connect( loadJob, SIGNAL(documentLoaded(Kasten2::AbstractDocument*)),
105  SLOT(onDocumentLoaded(Kasten2::AbstractDocument*)) );
106 
107  JobManager::executeJob( loadJob ); // TODO: pass a ui handler to jobmanager
108 
109  // store path
110 // mWorkingUrl = url.upUrl();
111  emit urlUsed( url );
112 }
113 
120 static QString mimetypeFilterString( const QStringList& _mimetypes )
121 {
122  QStringList mimetypes = _mimetypes;
123 
124  const int index = mimetypes.indexOf( QLatin1String("application/octet-stream") );
125  if( index != -1 )
126  mimetypes.replace( index, QLatin1String("all/allfiles") );
127 
128  return mimetypes.join( QLatin1String(" ") );
129 }
130 
131 bool DocumentSyncManager::setSynchronizer( AbstractDocument* document )
132 {
133  bool storingDone = false;
134 
135  AbstractModelSynchronizer* currentSynchronizer = document->synchronizer();
136  // TODO: warn if there were updates in the second before saveAs was activated
137 // if( currentSynchronizer )
138 // currentSynchronizer->pauseSynchronization(); also unpause below
139  const QString processTitle =
140  i18nc( "@title:window", "Save As" );
141  const QString filterString = mimetypeFilterString( supportedRemoteTypes() );
142  do
143  {
144  KUrl newUrl = KFileDialog::getSaveUrl( /*mWorkingUrl.url()*/KUrl(), filterString, /*mWidget*/0, processTitle );
145 
146  if( !newUrl.isEmpty() )
147  {
148  const bool isNewUrl = ( currentSynchronizer == 0 )
149  || ( newUrl != currentSynchronizer->url() );
150 
151  if( isNewUrl )
152  {
153  const bool isUrlInUse =
154  KIO::NetAccess::exists( newUrl, KIO::NetAccess::DestinationSide, /*mWidget*/0 );
155 
156  if( isUrlInUse )
157  {
158  // TODO: care for case that file from url is already loaded by (only?) this program
159 // const bool otherFileLoaded = mManager->documentByUrl( newUrl );
160  // TODO: replace "file" with synchronizer->storageTypeName() or such
161  // TODO: offer "Synchronize" as alternative, if supported, see below
162  // ask synchronizer for capabilities, as some can only overwrite
163  const Answer answer =
164  mOverwriteDialog ? mOverwriteDialog->queryOverwrite( newUrl, processTitle ) : Cancel;
165 
166  if( answer == Cancel )
167  break;
168  if( answer == PreviousQuestion )
169  continue;
170  }
171 
172  // switch url and synchronizer
173  if( currentSynchronizer && true )//TODO: same remote mimetype
174  {
175  //TODO: overwrite for now
176  AbstractSyncWithRemoteJob* syncJob = currentSynchronizer->startSyncWithRemote( newUrl,
177  AbstractModelSynchronizer::ReplaceRemote );
178  const bool syncSucceeded = JobManager::executeJob( syncJob );
179 // currentSynchronizer->unpauseSynchronization(); also pause above
180  storingDone = syncSucceeded;
181  }
182  else
183  {
184  //TODO: is overwrite for now, is this useful?
185  AbstractModelSynchronizer* synchronizer = mSynchronizerFactory->createSynchronizer();
186  AbstractConnectJob* connectJob = synchronizer->startConnect( document, newUrl,
187  AbstractModelSynchronizer::ReplaceRemote );
188  const bool connectSucceeded = JobManager::executeJob( connectJob );
189 
190  storingDone = connectSucceeded;
191  }
192 
193  if( storingDone )
194  emit urlUsed( newUrl );
195 #if 0
196 // mWorkingUrl = Url.upUrl();
197  OpenRecentAction->addUrl( Url );
198 #endif
199  }
200  // same url
201  else
202  {
203  // TODO: what to do? synchTo? synchWith? synchFrom? Or does the synchronizer care for this?
204  // By e.g. warning that we might be overwriting something?
205  // synchTo might be the intention, after all the user wanted a new storage
206  //
207  AbstractSyncToRemoteJob* syncJob = document->synchronizer()->startSyncToRemote();
208  const bool syncFailed = JobManager::executeJob( syncJob );
209 
210  storingDone = !syncFailed;
211  }
212  }
213  else
214  break;
215  }
216  while( !storingDone );
217 
218  return storingDone;
219 }
220 
221 bool DocumentSyncManager::canClose( AbstractDocument* document )
222 {
223  bool canClose = true;
224 
225  if( document->contentFlags() & ContentHasUnstoredChanges )
226  {
227  AbstractModelSynchronizer* synchronizer = document->synchronizer();
228  const bool couldSynchronize = hasSynchronizerForLocal( document->mimeType() );
229 
230  const QString processTitle = i18nc( "@title:window", "Close" );
231 
232  if( (synchronizer && synchronizer->localSyncState() == LocalHasChanges) ||
233  couldSynchronize )
234  {
235  const Answer answer =
236  mSaveDiscardDialog ? mSaveDiscardDialog->querySaveDiscard( document, processTitle ) : Cancel;
237 
238  if( answer == Save )
239  {
240  if( synchronizer )
241  {
242  AbstractSyncToRemoteJob* syncJob = synchronizer->startSyncToRemote();
243  const bool isSynced = JobManager::executeJob( syncJob );
244 
245  canClose = isSynced;
246  }
247  else
248  canClose = setSynchronizer( document );
249  }
250  else
251  canClose = ( answer == Discard );
252  }
253  else
254  {
255  const Answer answer =
256  mSaveDiscardDialog ? mSaveDiscardDialog->queryDiscard( document, processTitle ) : Cancel;
257 
258  canClose = ( answer == Discard );
259  }
260  }
261 
262  return canClose;
263 }
264 
265 void DocumentSyncManager::reload( AbstractDocument* document )
266 {
267  AbstractModelSynchronizer* synchronizer = document->synchronizer();
268 
269  if( synchronizer->localSyncState() == LocalHasChanges )
270  {
271  const QString processTitle = i18nc( "@title:window", "Reload" );
272 
273  const Answer answer =
274  mSaveDiscardDialog ? mSaveDiscardDialog->queryDiscardOnReload( document, processTitle ) : Cancel;
275 
276  if( answer == Cancel )
277  return;
278  }
279 
280  AbstractSyncFromRemoteJob* syncJob = synchronizer->startSyncFromRemote();
281  JobManager::executeJob( syncJob );
282 }
283 
284 void DocumentSyncManager::save( AbstractDocument* document )
285 {
286  AbstractModelSynchronizer* synchronizer = document->synchronizer();
287  AbstractSyncToRemoteJob* syncJob = synchronizer->startSyncToRemote();
288  JobManager::executeJob( syncJob );
289 }
290 
291 void DocumentSyncManager::onDocumentLoaded( AbstractDocument* document )
292 {
293  if( document )
294  mManager->addDocument( document );
295 }
296 
297 void DocumentSyncManager::onDocumentsAdded( const QList<Kasten2::AbstractDocument*>& documents )
298 {
299  Q_UNUSED( documents )
300 }
301 
302 void DocumentSyncManager::onDocumentsClosing( const QList<Kasten2::AbstractDocument*>& documents )
303 {
304  Q_UNUSED( documents )
305 }
306 
307 
308 DocumentSyncManager::~DocumentSyncManager()
309 {
310  delete mSynchronizerFactory;
311 }
312 
313 }
abstractconnectjob.h
abstractdocument.h
documentsyncmanager.h
Kasten2::Discard
Definition: kastencore.h:69
Kasten2::DocumentSyncManager::DocumentSyncManager
DocumentSyncManager(DocumentManager *manager)
Definition: documentsyncmanager.cpp:49
abstractloadjob.h
Kasten2::AbstractModelSynchronizer::startConnect
virtual AbstractConnectJob * startConnect(AbstractDocument *document, const KUrl &url, AbstractModelSynchronizer::ConnectOption option)=0
Kasten2::DocumentSyncManager::setSynchronizer
bool setSynchronizer(AbstractDocument *document)
Definition: documentsyncmanager.cpp:131
Kasten2::DocumentSyncManager::load
void load(const KUrl &url)
Definition: documentsyncmanager.cpp:90
abstractsavediscarddialog.h
Kasten2::DocumentManager::addDocument
void addDocument(AbstractDocument *document)
Definition: documentmanager.cpp:55
documentmanager.h
abstractsyncwithremotejob.h
Kasten2::DocumentSyncManager::setOverwriteDialog
void setOverwriteDialog(AbstractOverwriteDialog *overwriteDialog)
Definition: documentsyncmanager.cpp:61
Kasten2::DocumentSyncManager::urlUsed
void urlUsed(const KUrl &url)
Kasten2::DocumentSyncManager::urlOf
KUrl urlOf(AbstractDocument *document) const
Definition: documentsyncmanager.cpp:78
Kasten2::AbstractModelSynchronizer::startSyncFromRemote
virtual AbstractSyncFromRemoteJob * startSyncFromRemote()=0
overwrite local with remote (reload)
Kasten2::DocumentManager::documents
QList< AbstractDocument * > documents() const
Definition: documentmanager.cpp:52
jobmanager.h
Kasten2::AbstractSaveDiscardDialog::queryDiscard
virtual Answer queryDiscard(const AbstractDocument *document, const QString &title) const =0
Kasten2::AbstractSyncToRemoteJob
Definition: abstractsynctoremotejob.h:37
Kasten2::AbstractModelSynchronizer::startSyncWithRemote
virtual AbstractSyncWithRemoteJob * startSyncWithRemote(const KUrl &url, AbstractModelSynchronizer::ConnectOption option)=0
changes the
Kasten2::AbstractOverwriteDialog::queryOverwrite
virtual Answer queryOverwrite(const KUrl &url, const QString &title) const =0
Kasten2::AbstractSaveDiscardDialog::querySaveDiscard
virtual Answer querySaveDiscard(const AbstractDocument *document, const QString &title) const =0
Kasten2::ContentHasUnstoredChanges
Definition: kastencore.h:53
Kasten2::AbstractConnectJob
Definition: abstractconnectjob.h:38
Kasten2::Save
Definition: kastencore.h:67
Kasten2::AbstractLoadJob
Definition: abstractloadjob.h:40
Kasten2::AbstractDocument::mimeType
virtual QString mimeType() const =0
Kasten2::PreviousQuestion
Definition: kastencore.h:65
Kasten2::AbstractModelSynchronizerFactory::createSynchronizer
virtual AbstractModelSynchronizer * createSynchronizer() const =0
abstractoverwritedialog.h
Kasten2::AbstractModelSynchronizerFactory::supportedWorkType
virtual QString supportedWorkType() const =0
returns the id of the work model type
Kasten2::DocumentManager::focusRequested
void focusRequested(Kasten2::AbstractDocument *document)
Kasten2::DocumentSyncManager::supportedRemoteTypes
QStringList supportedRemoteTypes() const
Definition: documentsyncmanager.cpp:67
Kasten2::AbstractModelSynchronizer::localSyncState
virtual LocalSyncState localSyncState() const =0
Kasten2::AbstractModelSynchronizer::url
KUrl url() const
Definition: abstractmodelsynchronizer.cpp:40
Kasten2::Answer
Answer
Definition: kastencore.h:58
abstractsynctoremotejob.h
Kasten2::AbstractModelSynchronizerFactory
Definition: abstractmodelsynchronizerfactory.h:42
Kasten2::AbstractDocument::contentFlags
virtual ContentFlags contentFlags() const =0
Kasten2::AbstractModelSynchronizerFactory::supportedRemoteType
virtual QString supportedRemoteType() const =0
returns the id of the remote model type
Kasten2::Cancel
Definition: kastencore.h:60
Kasten2::AbstractSaveDiscardDialog
Definition: abstractsavediscarddialog.h:38
Kasten2::AbstractModelSynchronizer::startSyncToRemote
virtual AbstractSyncToRemoteJob * startSyncToRemote()=0
overwrite remote with local (save)
Kasten2::DocumentSyncManager::save
void save(AbstractDocument *document)
Definition: documentsyncmanager.cpp:284
Kasten2::AbstractDocument
Definition: abstractdocument.h:43
abstractsyncfromremotejob.h
Kasten2::DocumentSyncManager::setSaveDiscardDialog
void setSaveDiscardDialog(AbstractSaveDiscardDialog *saveDiscardDialog)
Definition: documentsyncmanager.cpp:56
Kasten2::AbstractModelSynchronizer::startLoad
virtual AbstractLoadJob * startLoad(const KUrl &url)=0
Kasten2::JobManager::executeJob
static bool executeJob(KJob *job)
Definition: jobmanager.cpp:35
Kasten2::AbstractModelSynchronizer
possible actions:
Definition: abstractmodelsynchronizer.h:61
Kasten2::AbstractModelSynchronizer::ReplaceRemote
Definition: abstractmodelsynchronizer.h:69
Kasten2::AbstractSyncFromRemoteJob
Definition: abstractsyncfromremotejob.h:38
Kasten2::DocumentSyncManager::hasSynchronizerForLocal
bool hasSynchronizerForLocal(const QString &mimeType) const
Definition: documentsyncmanager.cpp:72
Kasten2::AbstractSyncWithRemoteJob
Definition: abstractsyncwithremotejob.h:38
Kasten2::AbstractDocument::synchronizer
AbstractModelSynchronizer * synchronizer() const
Definition: abstractdocument.cpp:40
abstractmodelsynchronizerfactory.h
Kasten2::LocalHasChanges
Definition: kastencore.h:36
Kasten2::DocumentSyncManager::canClose
bool canClose(AbstractDocument *document)
Definition: documentsyncmanager.cpp:221
Kasten2::DocumentSyncManager::setDocumentSynchronizerFactory
void setDocumentSynchronizerFactory(AbstractModelSynchronizerFactory *synchronizerFactory)
Definition: documentsyncmanager.cpp:85
Kasten2::DocumentManager
Definition: documentmanager.h:44
Kasten2::AbstractOverwriteDialog
Definition: abstractoverwritedialog.h:37
Kasten2::DocumentSyncManager::reload
void reload(AbstractDocument *document)
Definition: documentsyncmanager.cpp:265
Kasten2::mimetypeFilterString
static QString mimetypeFilterString(const QStringList &_mimetypes)
Creates a filter string as used by KFileDialog from _mimetypes Does a workaround for "application/oct...
Definition: loadercontroller.cpp:73
QList< Kasten2::AbstractDocument * >
Kasten2::AbstractSaveDiscardDialog::queryDiscardOnReload
virtual Answer queryDiscardOnReload(const AbstractDocument *document, const QString &title) const =0
Kasten2::DocumentSyncManager::~DocumentSyncManager
virtual ~DocumentSyncManager()
Definition: documentsyncmanager.cpp:308
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

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

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

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