/*
    This file is part of ksync.

    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/
#ifndef KSYNCVIEW_H
#define KSYNCVIEW_H

#include <qptrlist.h>
#include <qwidget.h>

#include <ktempfile.h>
#include <kurl.h>
#include <kio/job.h>
#include <kdebug.h>

#include <ksyncer.h>
#include <kdepimmacros.h>

class QListView;
class QCheckBox;
class QComboBox;
class QPushButton;
class KConfig;
class KURLRequester;

class KSyncer;


/** The KSyncView class provides the view widget for the KSyncApp instance.	
 * The View instance inherits QWidget as a base class and represents the view object of a KTMainWindow. As KSyncView is part of the
 * docuement-view model, it needs a reference to the document object connected with it by the KSyncApp class to manipulate and display
 * the document structure provided by the KSyncDoc class.
 * 	
 * @author Source Framework Automatically Generated by KDevelop, (c) The KDevelop Team.
 * @version KDevelop version 0.4 code generation
 */
class KDE_EXPORT KSyncView : public QWidget
{
    Q_OBJECT
  public:
    /** Constructor for the main view */
    KSyncView(QWidget *parent = 0, const char *name=0);
    /** Destructor for the main view */
    ~KSyncView();

    enum { TypeCalendar, TypeBookmarks, TypeAddressBook };

    /** contains the implementation for printing functionality */
    void print(QPrinter *pPrinter);

    void readConfig(KConfig *);
    void readTypeConfig(KConfig *);
    void writeConfig(KConfig *);
    void writeTypeConfig(KConfig *);

    void synceeLoadError();
    void synceeLoaded();

  private slots:
    void addSource();
    void removeSource();
    void showSource();
    void doSync();
    void checkSyncBack();
    void reloadSyncees();
    void finishSync();
    void jobFinished(KIO::Job *job);
    void slotSelectionChanged();  
  private:
    void checkFinish();
    KSyncee *createSyncee(const KURL &);
    QString createTempFile();
    
    QListView *mSourceListView;
    QComboBox *mTypeCombo;
    KURLRequester *mTargetReq;
    QCheckBox *mSyncBackCheck;
    
    int mCurrentType;
    
    KSyncer *mSyncer;
    KSyncee *mTarget;
    QPtrList<KTempFile> mTmpFiles;
    int mLoadCount;
    bool mLoadError;
    QPushButton *showButton,*removeButton;
};


class SynceeLoader : public QObject
{
    Q_OBJECT
  public:
    SynceeLoader(KSyncView *parent,KSyncee *syncee,const QString &filename) :
      mSyncee(syncee), mParent(parent)
    {
      mSyncee->setFilename(filename);
    }
    virtual ~SynceeLoader() {}
    
  public slots:
    void loadSyncee(KIO::Job *job)
    {
      if (job->error()) {
        job->showErrorDialog(mParent);
        mParent->synceeLoadError();
      } else {
        loadSyncee();
        mParent->synceeLoaded();
      }
      delete this;
    }
    
    void loadSyncee()
    {
      kdDebug() << "SynceeLoader::loadSyncee(): " << mSyncee->filename()
                << endl;
      if (!mSyncee->load()) {
        mParent->synceeLoadError();
      }
    }
    
  private:
    KSyncee *mSyncee;
    KSyncView *mParent;
};

#endif // KSYNCVIEW_H
