00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
#include <qprinter.h>
00023
#include <qpainter.h>
00024
#include <qlayout.h>
00025
#include <qlabel.h>
00026
#include <qlistview.h>
00027
#include <qcombobox.h>
00028
#include <qpushbutton.h>
00029
#include <qcheckbox.h>
00030
00031
#include <klocale.h>
00032
#include <kurlrequester.h>
00033
#include <kfiledialog.h>
00034
#include <kdebug.h>
00035
#include <kio/job.h>
00036
#include <kio/jobclasses.h>
00037
#include <kmessagebox.h>
00038
00039
#include <ksyncer.h>
00040
#include <calendarsyncee.h>
00041
#include <bookmarksyncee.h>
00042
#include <addressbooksyncee.h>
00043
#include <ksyncuikde.h>
00044
00045
#include "ksync.h"
00046
00047
#include "ksyncview.h"
00048
#include "ksyncview.moc"
00049
00050
class SynceeListItem :
public QListViewItem {
00051
public:
00052 SynceeListItem(QListView *lv,KURL url) : QListViewItem(lv,url.url()),
00053 mUrl(url) {}
00054
00055
void setSyncee(
KSyncee *syncee) { mSyncee = syncee; }
00056
KSyncee *syncee() {
return mSyncee; }
00057
00058 KURL url() {
return mUrl; }
00059
00060
private:
00061
KSyncee *mSyncee;
00062 KURL mUrl;
00063 };
00064
00065 KSyncView::KSyncView(QWidget *parent,
const char *name) :
00066 QWidget(parent, name),
00067 mSyncer(0), mTarget(0), mLoadError(false)
00068 {
00069 mTmpFiles.setAutoDelete(
true);
00070
00071 QLabel *typeLabel =
new QLabel(i18n(
"Data type to be synced:"),
this);
00072
00073 mTypeCombo =
new QComboBox(
this);
00074 mTypeCombo->insertItem(i18n(
"Calendar"),TypeCalendar);
00075 mTypeCombo->insertItem(i18n(
"Bookmarks"),TypeBookmarks);
00076 mTypeCombo->insertItem(i18n(
"Addressbook"),TypeAddressBook);
00077 mCurrentType = mTypeCombo->currentItem();
00078 connect(mTypeCombo,SIGNAL(activated(
int)),SLOT(reloadSyncees()));
00079
00080 QPushButton *addButton =
new QPushButton(i18n(
"Add Source..."),
this);
00081 connect(addButton,SIGNAL(clicked()),SLOT(addSource()));
00082
00083 removeButton =
new QPushButton(i18n(
"Remove Source"),
this);
00084 connect(removeButton,SIGNAL(clicked()),SLOT(removeSource()));
00085
00086 showButton =
new QPushButton(i18n(
"Show Source"),
this);
00087 connect(showButton,SIGNAL(clicked()),SLOT(showSource()));
00088
00089 mSourceListView =
new QListView(
this);
00090 mSourceListView->addColumn(i18n(
"URL"));
00091
00092 connect(mSourceListView,SIGNAL(selectionChanged ()),
this,SLOT(slotSelectionChanged()));
00093 mSyncBackCheck =
new QCheckBox(i18n(
"Write synced data back to sources."),
00094
this);
00095 connect(mSyncBackCheck,SIGNAL(clicked()),SLOT(checkSyncBack()));
00096
00097 QLabel *targetLabel =
new QLabel(i18n(
"Target: "),
this);
00098 mTargetReq =
new KURLRequester(
this);
00099
00100 QPushButton *syncButton =
new QPushButton(i18n(
"Sync"),
this);
00101 connect(syncButton,SIGNAL(clicked()),SLOT(doSync()));
00102
00103 checkSyncBack();
00104
00105 QGridLayout *topLayout =
new QGridLayout(
this);
00106 topLayout->setMargin(8);
00107 topLayout->setSpacing(8);
00108 topLayout->addWidget(typeLabel,0,0);
00109 topLayout->addMultiCellWidget(mTypeCombo,0,0,1,2);
00110 topLayout->addMultiCellWidget(addButton,1,1,0,0);
00111 topLayout->addMultiCellWidget(removeButton,1,1,1,1);
00112 topLayout->addMultiCellWidget(showButton,1,1,2,2);
00113 topLayout->addMultiCellWidget(mSourceListView,2,2,0,2);
00114 topLayout->addMultiCellWidget(mSyncBackCheck,3,3,0,2);
00115 topLayout->addMultiCellWidget(targetLabel,4,4,0,0);
00116 topLayout->addMultiCellWidget(mTargetReq,4,4,1,2);
00117 topLayout->addMultiCellWidget(syncButton,5,5,0,2);
00118 slotSelectionChanged();
00119 }
00120
00121 KSyncView::~KSyncView()
00122 {
00123 }
00124
00125 void KSyncView::print(QPrinter *pPrinter)
00126 {
00127 QPainter printpainter;
00128 printpainter.begin(pPrinter);
00129
00130
00131
00132 printpainter.end();
00133 }
00134
00135
void KSyncView::addSource()
00136 {
00137 KURL url = KFileDialog::getOpenURL();
00138
if(!url.path().isEmpty())
00139
new SynceeListItem(mSourceListView,url);
00140 }
00141
00142
void KSyncView::removeSource()
00143 {
00144 QListViewItem *item = mSourceListView->selectedItem();
00145
if (item)
delete item;
00146 }
00147
00148
void KSyncView::showSource()
00149 {
00150 QListViewItem *item = mSourceListView->selectedItem();
00151
if (!item) {
00152 kdDebug() <<
"No item selected" << endl;
00153
return;
00154 }
else {
00155 kdDebug() <<
"** Source '" << item->text(0) << endl;
00156
KSyncee *syncee = createSyncee(KURL( item->text(0) ));
00157
KSyncEntry *entry = syncee->
firstEntry();
00158
while(entry) {
00159 kdDebug() <<
"** '" << entry->
name() <<
"'" << endl;
00160
00161 entry = syncee->
nextEntry();
00162 }
00163
delete syncee;
00164 }
00165 }
00166
00167
void KSyncView::slotSelectionChanged()
00168 {
00169
bool state=(mSourceListView->currentItem()!=0);
00170 showButton->setEnabled(state);
00171 removeButton->setEnabled(state);
00172 }
00173
00174
void KSyncView::doSync()
00175 {
00176
delete mSyncer;
00177 mSyncer =
new KSyncer(
new KSyncUiKde(
this));
00178
00179 mLoadCount = 0;
00180 mLoadError =
false;
00181
00182 SynceeListItem *item = dynamic_cast<SynceeListItem *>(mSourceListView->firstChild());
00183
while(item) {
00184
KSyncee *syncee = createSyncee(item->url());
00185 item->setSyncee(syncee);
00186 mSyncer->
addSyncee(syncee);
00187
00188 item = (SynceeListItem *)item->nextSibling();
00189 }
00190
00191 QString url = mTargetReq->url();
00192
00193 kdDebug() <<
"target url: " << url << endl;
00194 mTarget = createSyncee(KURL(url));
00195
00196 finishSync();
00197 }
00198
00199
KSyncee *KSyncView::createSyncee(
const KURL &url)
00200 {
00201 kdDebug() <<
"KSyncView::createSyncee(): " << url.url() << endl;
00202
00203
KSyncee *syncee;
00204
switch (mTypeCombo->currentItem()) {
00205
case TypeBookmarks:
00206 syncee =
new BookmarkSyncee();
00207
break;
00208
case TypeAddressBook:
00209 syncee =
new AddressBookSyncee();
00210
break;
00211
case TypeCalendar:
00212
default:
00213 syncee =
new CalendarSyncee();
00214
break;
00215 }
00216
00217 SynceeLoader *loader;
00218
if (url.isLocalFile()) {
00219 loader =
new SynceeLoader(
this,syncee,url.path());
00220 loader->loadSyncee();
00221 ++mLoadCount;
00222
delete loader;
00223
return syncee;
00224 }
else {
00225 QString tmpFile = createTempFile();
00226
00227 loader =
new SynceeLoader(
this,syncee,tmpFile);
00228 KIO::FileCopyJob *j = KIO::file_copy(url,KURL( tmpFile ),-1,
true);
00229 connect(j,SIGNAL(result(KIO::Job *)),
00230 loader,SLOT(loadSyncee(KIO::Job *)));
00231
return syncee;
00232 }
00233 }
00234
00235
void KSyncView::synceeLoaded()
00236 {
00237 ++mLoadCount;
00238 finishSync();
00239 }
00240
00241
void KSyncView::finishSync()
00242 {
00243 kdDebug() <<
"KSyncView::finishSync()" << endl;
00244
00245
if (mLoadError) {
00246 kdDebug() <<
"KSyncView::finishSync(): load error" << endl;
00247
return;
00248 }
00249
00250
if (mLoadCount == mSourceListView->childCount() + 1) {
00251 mLoadCount = 0;
00252
if (mSyncBackCheck->isChecked()) {
00253 mSyncer->
sync();
00254 SynceeListItem *item = dynamic_cast<SynceeListItem *>(mSourceListView->firstChild());
00255 KIO::FileCopyJob *j;
00256
while(item) {
00257 KURL from(item->syncee()->filename());
00258 KURL to(item->url());
00259
if (from != to) {
00260 kdDebug() <<
"Copy " << from.url() <<
" to " << to.url() << endl;
00261 j = KIO::file_copy(from,to,-1,
true);
00262 connect(j,SIGNAL(result(KIO::Job *)),SLOT(jobFinished(KIO::Job *)));
00263 }
else {
00264 checkFinish();
00265 }
00266
00267 item = (SynceeListItem *)item->nextSibling();
00268 }
00269 }
else {
00270 mSyncer->
syncAllToTarget(mTarget);
00271 mTarget->
save();
00272 }
00273 }
else {
00274 kdDebug() <<
"KSyncView::finishSync(): loadCount: " << mLoadCount <<
" childCount: "
00275 << mSourceListView->childCount() + 1 << endl;
00276 }
00277 }
00278
00279
void KSyncView::jobFinished(KIO::Job *job)
00280 {
00281
if (job->error()) {
00282 job->showErrorDialog(
this);
00283 }
else {
00284 checkFinish();
00285 }
00286 }
00287
00288
void KSyncView::checkFinish()
00289 {
00290 ++mLoadCount;
00291
if (mLoadCount == mSourceListView->childCount()) {
00292 KMessageBox::information(
this,i18n(
"Sync finished"));
00293 mLoadCount = 0;
00294 }
00295 }
00296
00297
void KSyncView::synceeLoadError()
00298 {
00299 kdDebug() <<
"KSyncView::synceeLoadError()" << endl;
00300
00301 mLoadError =
true;
00302
00303 KMessageBox::error(
this,i18n(
"Can't load syncee."),i18n(
"Load Error"));
00304 }
00305
00306
void KSyncView::readConfig(KConfig *config)
00307 {
00308
int typeIndex = config->readNumEntry(
"SyncType",TypeCalendar);
00309 mTypeCombo->setCurrentItem(typeIndex);
00310 mCurrentType = typeIndex;
00311
00312 readTypeConfig(config);
00313 }
00314
00315
void KSyncView::readTypeConfig(KConfig *config)
00316 {
00317 QString typeString = mTypeCombo->text(mCurrentType);
00318
00319 QStringList sources = config->readPathListEntry(
"Sources_" + typeString);
00320
00321 mSourceListView->clear();
00322 QStringList::ConstIterator it = sources.begin();
00323
while(it != sources.end()) {
00324
new SynceeListItem (mSourceListView,KURL(*it));
00325 ++it;
00326 }
00327
00328 mTargetReq->setURL(config->readPathEntry(
"Target_" + typeString));
00329 }
00330
00331
void KSyncView::writeConfig(KConfig *config)
00332 {
00333 config->writeEntry(
"SyncType",mTypeCombo->currentItem());
00334
00335 writeTypeConfig(config);
00336 }
00337
00338
void KSyncView::writeTypeConfig(KConfig *config)
00339 {
00340 QStringList sources;
00341 QListViewItem *item = mSourceListView->firstChild();
00342
while(item) {
00343 sources.append(item->text(0));
00344 item = item->nextSibling();
00345 }
00346
00347 QString typeString = mTypeCombo->text(mCurrentType);
00348
00349 config->writePathEntry(
"Sources_" + typeString,sources);
00350 config->writePathEntry(
"Target_" + typeString,mTargetReq->url());
00351
00352 config->sync();
00353 }
00354
00355
void KSyncView::checkSyncBack()
00356 {
00357
if (mSyncBackCheck->isChecked()) {
00358 mTargetReq->setEnabled(
false);
00359 }
else {
00360 mTargetReq->setEnabled(
true);
00361 }
00362 }
00363
00364
void KSyncView::reloadSyncees()
00365 {
00366 KConfig *config = kapp->config();
00367
00368 writeTypeConfig(config);
00369 mCurrentType = mTypeCombo->currentItem();
00370 readTypeConfig(config);
00371 }
00372
00373 QString KSyncView::createTempFile()
00374 {
00375 KTempFile *tmpFile =
new KTempFile;
00376 mTmpFiles.append(tmpFile);
00377 tmpFile->setAutoDelete(
true);
00378
return tmpFile->name();
00379 }