kitchensync

mainwidget.cpp
1 /*
2  This file is part of KitchenSync.
3 
4  Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
5  Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21 
22 #include "mainwidget.h"
23 
24 #include "groupconfigdialog.h"
25 #include "groupview.h"
26 #include "syncprocess.h"
27 #include "syncprocessmanager.h"
28 
29 #include <libqopensync/environment.h>
30 
31 #include <tdeaboutdata.h>
32 #include <tdeaction.h>
33 #include <kdebug.h>
34 #include <kinputdialog.h>
35 #include <tdelistview.h>
36 #include <tdelocale.h>
37 #include <tdemessagebox.h>
38 #include <kstdaction.h>
39 #include <kxmlguiclient.h>
40 
41 #include <tqlayout.h>
42 
43 MainWidget::MainWidget( KXMLGUIClient *guiClient, TQWidget *widget, const char *name )
44  : TQWidget( widget, name ), mGUIClient( guiClient )
45 {
46  initGUI();
47  initActions();
48 
50  int count = SyncProcessManager::self()->count();
51  for ( int i = 0; i < count; ++i ) {
52  SyncProcessManager::self()->at( i )->applyObjectTypeFilter();
53  }
56  mGroupView->updateView();
57 
58  connect( SyncProcessManager::self(), TQT_SIGNAL( changed() ),
59  mGroupView, TQT_SLOT( updateView() ) );
60  connect( SyncProcessManager::self(), TQT_SIGNAL( syncProcessChanged( SyncProcess* ) ),
61  mGroupView, TQT_SLOT( updateSyncProcess( SyncProcess* ) ) );
62 
63  enableActions();
64 }
65 
66 MainWidget::~MainWidget()
67 {
68 }
69 
70 KXMLGUIClient *MainWidget::guiClient() const
71 {
72  return mGUIClient;
73 }
74 
75 TDEAboutData *MainWidget::aboutData()
76 {
77  TDEAboutData *about = new TDEAboutData( "kitchensync", I18N_NOOP( "KitchenSync" ),
78  "0.1", I18N_NOOP( "The TDE Syncing Application" ),
79  TDEAboutData::License_GPL_V2,
80  I18N_NOOP( "(c) 2005, The KDE PIM Team" ) );
81  about->addAuthor( "Tobias Koenig", I18N_NOOP( "Current maintainer" ), "tokoe@kde.org" );
82  about->addAuthor( "Cornelius Schumacher", 0, "schumacher@kde.org" );
83 
84  return about;
85 }
86 
87 void MainWidget::initGUI()
88 {
89  TQVBoxLayout *topLayout = new TQVBoxLayout( this );
90 
91  mGroupView = new GroupView( this );
92  topLayout->addWidget( mGroupView );
93 
94  connect( mGroupView, TQT_SIGNAL( addGroup() ), TQT_SLOT( addGroup() ) );
95  connect( mGroupView, TQT_SIGNAL( synchronizeGroup( SyncProcess* ) ),
96  TQT_SLOT( sync( SyncProcess* ) ) );
97  connect( mGroupView, TQT_SIGNAL( abortSynchronizeGroup( SyncProcess* ) ),
98  TQT_SLOT( abortSync( SyncProcess* ) ) );
99  connect( mGroupView, TQT_SIGNAL( configureGroup( SyncProcess* ) ),
100  TQT_SLOT( editGroup( SyncProcess* ) ) );
101 }
102 
103 void MainWidget::initActions()
104 {
105  mActionSynchronize = new TDEAction( i18n("Synchronize"), "hotsync", 0, TQT_TQOBJECT(this), TQT_SLOT( sync() ),
106  mGUIClient->actionCollection(), "sync" );
107  mActionAddGroup = new TDEAction( i18n("Add Group..."), "document-new", 0, TQT_TQOBJECT(this), TQT_SLOT( addGroup() ),
108  mGUIClient->actionCollection(), "add_group" );
109  mActionDeleteGroup = new TDEAction( i18n("Delete Group..."), "edit-delete", 0, TQT_TQOBJECT(this), TQT_SLOT( deleteGroup() ),
110  mGUIClient->actionCollection(), "delete_group" );
111  mActionEditGroup = new TDEAction( i18n("Edit Group..."), "edit", 0, TQT_TQOBJECT(this), TQT_SLOT( editGroup() ),
112  mGUIClient->actionCollection(), "edit_group" );
113 }
114 
115 void MainWidget::enableActions()
116 {
117  bool state = ( SyncProcessManager::self()->count() > 0 );
118 
119  mActionSynchronize->setEnabled( state );
120  mActionDeleteGroup->setEnabled( state );
121  mActionEditGroup->setEnabled( state );
122 }
123 
124 void MainWidget::addGroup()
125 {
126  bool ok;
127  TQString name = KInputDialog::getText( i18n("Create Synchronization Group"),
128  i18n("Name for new synchronization group."), TQString(), &ok, this );
129  if ( ok ) {
130  SyncProcessManager::self()->addGroup( name );
131  enableActions();
132 
133  SyncProcess *process = SyncProcessManager::self()->byGroupName( name );
134  if ( process )
135  editGroup( process );
136  }
137 }
138 
139 void MainWidget::deleteGroup()
140 {
141  SyncProcess *syncProcess = mGroupView->selectedSyncProcess();
142  if ( syncProcess ) {
143  int result = KMessageBox::warningContinueCancel( this,
144  i18n("Delete synchronization group '%1'?").arg( syncProcess->group().name() ) );
145  if ( result == KMessageBox::Continue ) {
146  SyncProcessManager::self()->remove( syncProcess );
147  enableActions();
148  }
149  }
150 }
151 
152 void MainWidget::editGroup()
153 {
154  editGroup( mGroupView->selectedSyncProcess() );
155 }
156 
157 void MainWidget::editGroup( SyncProcess *syncProcess )
158 {
159  if ( syncProcess ) {
160  GroupConfigDialog dlg( this, syncProcess );
161  dlg.exec();
162 
163  enableActions();
164  }
165 }
166 
167 void MainWidget::sync()
168 {
169  sync( mGroupView->selectedSyncProcess() );
170 }
171 
172 void MainWidget::sync( SyncProcess *syncProcess )
173 {
174  if ( syncProcess ) {
175  syncProcess->reinitEngine();
176  QSync::Result result = syncProcess->engine()->synchronize();
177  if ( result ) {
178  tqDebug( "%s", result.message().latin1() );
179  } else {
180  tqDebug( "synchronization worked" );
181  }
182  }
183 }
184 
185 void MainWidget::abortSync( SyncProcess *syncProcess )
186 {
187  if ( syncProcess )
188  syncProcess->engine()->abort();
189 }
190 
191 #include "mainwidget.moc"