summaryrefslogtreecommitdiffstats
path: root/kitchensync/src/mainwidget.cpp
blob: 51543c059cd1a4a0fa786f11fea2b137b65b3937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
    This file is part of KitchenSync.

    Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
    Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>

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

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "mainwidget.h"

#include "groupconfigdialog.h"
#include "groupview.h"
#include "syncprocess.h"
#include "syncprocessmanager.h"

#include <libqopensync/environment.h>

#include <kaboutdata.h>
#include <kaction.h>
#include <kdebug.h>
#include <kinputdialog.h>
#include <klistview.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstdaction.h>
#include <kxmlguiclient.h>

#include <tqlayout.h>

MainWidget::MainWidget( KXMLGUIClient *guiClient, TQWidget *widget, const char *name )
  : TQWidget( widget, name ), mGUIClient( guiClient )
{
  initGUI();
  initActions();

  /** apply object type filter hack **/
  int count = SyncProcessManager::self()->count();
  for ( int i = 0; i < count; ++i ) {
    SyncProcessManager::self()->at( i )->applyObjectTypeFilter();
  }
  /** apply object type filter hack **/

  mGroupView->updateView();

  connect( SyncProcessManager::self(), TQT_SIGNAL( changed() ),
           mGroupView, TQT_SLOT( updateView() ) );
  connect( SyncProcessManager::self(), TQT_SIGNAL( syncProcessChanged( SyncProcess* ) ),
           mGroupView, TQT_SLOT( updateSyncProcess( SyncProcess* ) ) );

  enableActions();
}

MainWidget::~MainWidget()
{
}

KXMLGUIClient *MainWidget::guiClient() const
{
  return mGUIClient;
}

KAboutData *MainWidget::aboutData()
{
  KAboutData *about = new KAboutData( "kitchensync", I18N_NOOP( "KitchenSync" ),
                                      "0.1", I18N_NOOP( "The KDE Syncing Application" ),
                                      KAboutData::License_GPL_V2,
                                      I18N_NOOP( "(c) 2005, The KDE PIM Team" ) );
  about->addAuthor( "Tobias Koenig", I18N_NOOP( "Current maintainer" ), "tokoe@kde.org" );
  about->addAuthor( "Cornelius Schumacher", 0, "schumacher@kde.org" );

  return about;
}

void MainWidget::initGUI()
{
  TQVBoxLayout *topLayout = new TQVBoxLayout( this );

  mGroupView = new GroupView( this );
  topLayout->addWidget( mGroupView );

  connect( mGroupView, TQT_SIGNAL( addGroup() ), TQT_SLOT( addGroup() ) );
  connect( mGroupView, TQT_SIGNAL( synchronizeGroup( SyncProcess* ) ),
           TQT_SLOT( sync( SyncProcess* ) ) );
  connect( mGroupView, TQT_SIGNAL( abortSynchronizeGroup( SyncProcess* ) ),
           TQT_SLOT( abortSync( SyncProcess* ) ) );
  connect( mGroupView, TQT_SIGNAL( configureGroup( SyncProcess* ) ),
           TQT_SLOT( editGroup( SyncProcess* ) ) );
}

void MainWidget::initActions()
{
  mActionSynchronize = new KAction( i18n("Synchronize"), "hotsync", 0, TQT_TQOBJECT(this), TQT_SLOT( sync() ),
                                    mGUIClient->actionCollection(), "sync" );
  mActionAddGroup = new KAction( i18n("Add Group..."), "filenew", 0, TQT_TQOBJECT(this), TQT_SLOT( addGroup() ),
                                 mGUIClient->actionCollection(), "add_group" );
  mActionDeleteGroup = new KAction( i18n("Delete Group..."), "editdelete", 0, TQT_TQOBJECT(this), TQT_SLOT( deleteGroup() ),
                                    mGUIClient->actionCollection(), "delete_group" );
  mActionEditGroup = new KAction( i18n("Edit Group..."), "edit", 0, TQT_TQOBJECT(this), TQT_SLOT( editGroup() ),
                                  mGUIClient->actionCollection(), "edit_group" );
}

void MainWidget::enableActions()
{
  bool state = ( SyncProcessManager::self()->count() > 0 );

  mActionSynchronize->setEnabled( state );
  mActionDeleteGroup->setEnabled( state );
  mActionEditGroup->setEnabled( state );
}

void MainWidget::addGroup()
{
  bool ok;
  TQString name = KInputDialog::getText( i18n("Create Synchronization Group"),
    i18n("Name for new synchronization group."), TQString(), &ok, this );
  if ( ok ) {
    SyncProcessManager::self()->addGroup( name );
    enableActions();

    SyncProcess *process = SyncProcessManager::self()->byGroupName( name );
    if ( process )
      editGroup( process );
  }
}

void MainWidget::deleteGroup()
{
  SyncProcess *syncProcess = mGroupView->selectedSyncProcess();
  if ( syncProcess ) {
    int result = KMessageBox::warningContinueCancel( this,
      i18n("Delete synchronization group '%1'?").arg( syncProcess->group().name() ) );
    if ( result == KMessageBox::Continue ) {
      SyncProcessManager::self()->remove( syncProcess );
      enableActions();
    }
  }
}

void MainWidget::editGroup()
{
  editGroup( mGroupView->selectedSyncProcess() );
}

void MainWidget::editGroup( SyncProcess *syncProcess )
{
  if ( syncProcess ) {
    GroupConfigDialog dlg( this, syncProcess );
    dlg.exec();

    enableActions();
  }
}

void MainWidget::sync()
{
  sync( mGroupView->selectedSyncProcess() );
}

void MainWidget::sync( SyncProcess *syncProcess )
{
  if ( syncProcess ) {
    syncProcess->reinitEngine();
    QSync::Result result = syncProcess->engine()->synchronize();
    if ( result ) {
      qDebug( "%s", result.message().latin1() );
    } else {
      qDebug( "synchronization worked" );
    }
  }
}

void MainWidget::abortSync( SyncProcess *syncProcess )
{
  if ( syncProcess )
    syncProcess->engine()->abort();
}

#include "mainwidget.moc"