00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "options.h"
00030
00031 #include <QtCore/QMap>
00032 #include <QtCore/QTimer>
00033 #include <QtGui/QGroupBox>
00034 #include <QtGui/QLabel>
00035 #include <QtGui/QLayout>
00036
00037 #include <kapplication.h>
00038 #include <kconfigskeleton.h>
00039 #include <kglobal.h>
00040 #include <klocale.h>
00041 #include <kmessagebox.h>
00042 #include <kprogressdialog.h>
00043 #include <kvbox.h>
00044
00045 #include "kpilotdevicelink.h"
00046 #include "pilotSysInfo.h"
00047 #include "pilotUser.h"
00048
00049 #include "kpilotConfig.h"
00050 #include "kpilot_daemon_interface.h"
00051
00052 #include "config_dialog_probe.moc"
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 ProbeDialog::ProbeDialog(QWidget *parent, const char *n) :
00088 KDialog(parent),
00089 fResultsGroup(0L),
00090 fStatusLabel(0L),
00091 fUserNameLabel(0L),
00092 fDeviceNameLabel(0L),
00093 fProgress(0L),
00094 fDetected(false), fUserName(), fDeviceName()
00095 {
00096 if (n)
00097 {
00098 setObjectName(n);
00099 }
00100 setCaption(i18n("Autodetecting Your Handheld"));
00101 setButtonText(Ok,i18n("Restart Detection"));
00102 setButtons(Ok|Cancel|User1);
00103 setDefaultButton(Cancel);
00104 setModal(true);
00105 FUNCTIONSETUP;
00106
00107 KVBox *mainWidget = new KVBox();
00108 setMainWidget(mainWidget);
00109
00110 QLabel *infoText = new QLabel( i18n( "KPilot is now trying to automatically detect the device of your handheld. Please press the hotsync button if you have not done so already." ), mainWidget );
00111 infoText->setObjectName( "fInfoText" );
00112 infoText->setWordWrap( true );
00113
00114 QGroupBox *statusGroup = new QGroupBox( i18n("Status"), mainWidget );
00115 QHBoxLayout *statusGroupLayout = new QHBoxLayout;
00116 statusGroup->setLayout( statusGroupLayout );
00117
00118 fStatusLabel = new QLabel( i18n("Autodetection not yet started..."), statusGroup );
00119 statusGroupLayout->addWidget( fStatusLabel );
00120
00121 fProgress = new QProgressBar( statusGroup );
00122 fProgress->setMaximum(100);
00123 statusGroupLayout->addWidget( fProgress );
00124
00125
00126
00127 fResultsGroup = new QGroupBox( i18n( "Detected Values" ), mainWidget );
00128 fResultsGroup->setEnabled( false );
00129 QGridLayout *resultsGroupLayout = new QGridLayout;
00130 fResultsGroup->setLayout( resultsGroupLayout );
00131
00132 QLabel *userLabel = new QLabel( i18n( "Handheld user:" ), fResultsGroup );
00133 resultsGroupLayout->addWidget( userLabel, 0, 0 );
00134
00135 QLabel *deviceLabel = new QLabel( i18n( "Device:" ), fResultsGroup );
00136 resultsGroupLayout->addWidget( deviceLabel, 1, 0 );
00137
00138 fUserNameLabel = new QLabel( i18n("[Not yet known]"), fResultsGroup );
00139 fUserNameLabel->setObjectName( "fUser" );
00140 resultsGroupLayout->addWidget( fUserNameLabel, 0, 1 );
00141
00142 fDeviceNameLabel = new QLabel( i18n("[Not yet known]"), fResultsGroup );
00143 fDeviceNameLabel->setObjectName( "fDevice" );
00144 resultsGroupLayout->addWidget( fDeviceNameLabel, 1, 1 );
00145
00146
00147 resize( QSize(459, 298).expandedTo(minimumSizeHint()) );
00148 enableButtonOk(false);
00149
00150 fDevicesToProbe[0] << "/dev/pilot";
00151 fDevicesToProbe[1] << "/dev/ttyS0"<< "/dev/ttyS2"
00152 << "/dev/tts/0"<< "/dev/tts/2"
00153 << "/dev/ttyUSB0"<< "/dev/ttyUSB2"
00154 << "/dev/usb/tts/0"<< "/dev/usb/tts/2"
00155 << "/dev/cuaa0"<< "/dev/cuaa2"
00156 << "/dev/cuad0"<< "/dev/cuad2"
00157 << "/dev/ucom0"<< "/dev/ucom2";
00158 fDevicesToProbe[2] << "/dev/ttyS1"<< "/dev/ttyS3"
00159 << "/dev/tts/1"<< "/dev/tts/3"
00160 << "/dev/ttyUSB1"<< "/dev/ttyUSB3"
00161 << "/dev/usb/tts/1"<< "/dev/usb/tts/3"
00162 << "/dev/cuaa1"<< "/dev/cuaa3"
00163 << "/dev/cuad1"<< "/dev/cuad3"
00164 << "/dev/ucom1"<< "/dev/ucom3";
00165
00166 fProcessEventsTimer = new QTimer( this );
00167 fTimeoutTimer = new QTimer( this );
00168 fProgressTimer = new QTimer( this );
00169 fRotateLinksTimer = new QTimer( this );
00170 connect( fProcessEventsTimer, SIGNAL(timeout()), this, SLOT(processEvents()) );
00171 connect( fTimeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()) );
00172 connect( fProgressTimer, SIGNAL(timeout()), this, SLOT( progress()) );
00173 connect( fRotateLinksTimer, SIGNAL(timeout()), this, SLOT( detect()) );
00174 connect( this, SIGNAL(finished()), this, SLOT(disconnectDevices()) );
00175 }
00176
00177 ProbeDialog::~ProbeDialog()
00178 {
00179 FUNCTIONSETUP;
00180 }
00181
00182 void ProbeDialog::processEvents()
00183 {
00184 FUNCTIONSETUP;
00185 KApplication::kApplication()->processEvents();
00186 }
00187
00188 void ProbeDialog::progress()
00189 {
00190 fProgress->setValue(fProgress->value()+1);
00191 }
00192
00193 int ProbeDialog::exec()
00194 {
00195 fDetected = false;
00196 fUserName.clear();
00197 fDeviceName.clear();
00198 QTimer::singleShot( 0, this, SLOT( startDetection() ) );
00199 return KDialog::exec();
00200 }
00201
00202 void ProbeDialog::startDetection()
00203 {
00204 FUNCTIONSETUP;
00205
00206 disconnectDevices();
00207 fProgress->setValue(0);
00208 fStatusLabel->setText( i18n("Starting detection...") );
00209 OrgKdeKpilotDaemonInterface *daemonInterface = new OrgKdeKpilotDaemonInterface("org.kde.kpilot.daemon", "/Daemon", QDBusConnection::sessionBus());
00210 if (daemonInterface) {
00211 daemonInterface->stopListening();
00212 }
00213 KPILOT_DELETE(daemonInterface);
00214 fTimeoutTimer->setSingleShot( true );
00215 fTimeoutTimer->start( 30000 );
00216 fProcessEventsTimer->setSingleShot( false );
00217 fProcessEventsTimer->start( 100 );
00218 fProgressTimer->setSingleShot( false );
00219 fProgressTimer->start( 300 );
00220
00221 KPilotDeviceLink*link;
00222 for (int i=0; i<3; ++i)
00223 {
00224 QStringList::iterator end(fDevicesToProbe[i].end());
00225 for (QStringList::iterator it=fDevicesToProbe[i].begin(); it!=end; ++it)
00226 {
00227 link = new KPilotDeviceLink();
00228 link->setDevice((*it));
00229 DEBUGKPILOT << "new kpilotDeviceLink for " << (*it);
00230 fDeviceLinks[i].append( link );
00231 connect( link, SIGNAL(deviceReady(KPilotLink*)), this, SLOT(connection(KPilotLink*)) );
00232 processEvents();
00233 }
00234 }
00235 fStatusLabel->setText( i18n("Waiting for handheld to connect...") );
00236 fProbeDevicesIndex=0;
00237
00238 detect();
00239 fRotateLinksTimer->setSingleShot( false );
00240 fRotateLinksTimer->start( 3000 );
00241 }
00242
00243
00244 void ProbeDialog::detect(int i)
00245 {
00246 FUNCTIONSETUP;
00247
00248 fProbeDevicesIndex = i;
00249 PilotLinkList::iterator end(fDeviceLinks[fProbeDevicesIndex].end());
00250
00251 for (PilotLinkList::iterator it=fDeviceLinks[fProbeDevicesIndex].begin(); it!=end; ++it)
00252 {
00253 if (*it)
00254 {
00255 (*it)->reset();
00256 }
00257 }
00258 }
00259
00260 void ProbeDialog::detect()
00261 {
00262 detect( (fProbeDevicesIndex+1)%3 );
00263 }
00264
00265 void ProbeDialog::timeout()
00266 {
00267 disconnectDevices();
00268 if (!fDetected) {
00269 fStatusLabel->setText( i18n("Timeout reached, could not detect a handheld.") );
00270 KMessageBox::information ( this
00271 , i18n("<qt>"
00272 "<p>A handheld could not be detected. Possible check the following "
00273 "things:</p>"
00274 "<ul>"
00275 "<li>Have you pressed the hotsync button on the handheld?</li>"
00276 "<li>Make sure the device sits in the cradle correctly.</li>"
00277 "<li>Make sure the cradle is correctly plugged in to the computer.</li>"
00278 "<li>Have you checked that your device is actually supported by kpilot"
00279 " (see http://www.kpilot.org).</li>"
00280 "</ul></qt>"
00281 ), i18n("Automatic Detection Failed"), "AutoDetectionFailed");
00282 }
00283 }
00284
00285 void ProbeDialog::connection( KPilotLink *lnk)
00286 {
00287 FUNCTIONSETUP;
00288
00289 fActiveLink = static_cast<KPilotDeviceLink*>(lnk);
00290 if ( !fActiveLink )
00291 {
00292 return;
00293 }
00294 const KPilotUser &usr( fActiveLink->getPilotUser() );
00295
00296 fUserName = usr.name();
00297 fDeviceName = fActiveLink->pilotPath();
00298
00299 fStatusLabel->setText( i18n("Found a connected device on %1",fDeviceName) );
00300 fUserNameLabel->setText( fUserName );
00301 fDeviceNameLabel->setText( fDeviceName );
00302 fDetected = true;
00303
00304 fResultsGroup->setEnabled( true );
00305 enableButtonOk(true);
00306
00307 QTimer::singleShot(0, this, SLOT(retrieveDBList()));
00308 }
00309
00310 void ProbeDialog::retrieveDBList()
00311 {
00312 KPilotLink::DBInfoList dbs = fActiveLink->getDBList();
00313 fDBs.clear();
00314 char buff[7];
00315 buff[0] = '[';
00316
00317 for ( KPilotLink::DBInfoList::ConstIterator i = dbs.begin(); i != dbs.end(); ++i )
00318 {
00319 set_long( &buff[1], (*i).creator );
00320 buff[5] = ']';
00321 buff[6] = '\0';
00322 QString cr( buff );
00323 fDBs << cr;
00324 fDBs << QString( (*i).name );
00325 }
00326 fDBs.sort();
00327
00328 QString old;
00329 QStringList::Iterator itr = fDBs.begin();
00330 while ( itr != fDBs.end() )
00331 {
00332 if ( old == *itr )
00333 {
00334 itr = fDBs.erase( itr );
00335 }
00336 else
00337 {
00338 old = *itr;
00339 ++itr;
00340 }
00341 }
00342
00343
00344 fActiveLink->endSync( KPilotLink::NoUpdate );
00345
00346 QTimer::singleShot(0, this, SLOT(disconnectDevices()));
00347 }
00348 void ProbeDialog::disconnectDevices()
00349 {
00350 FUNCTIONSETUP;
00351
00352 if (!fDetected)
00353 {
00354 fStatusLabel->setText( i18n("Disconnected from all devices") );
00355 }
00356 fProcessEventsTimer->stop( );
00357 fTimeoutTimer->stop();
00358 fProgressTimer->stop();
00359 fRotateLinksTimer->stop();
00360 fProgress->setValue(fProgress->maximum());
00361 for (int i=0; i<3; ++i)
00362 {
00363 PilotLinkList::iterator end(fDeviceLinks[i].end());
00364 for (PilotLinkList::iterator it=fDeviceLinks[i].begin(); it!=end; ++it)
00365 {
00366 (*it)->close();
00367 KPILOT_DELETE(*it);
00368 }
00369 fDeviceLinks[i].clear();
00370 }
00371
00372
00373 OrgKdeKpilotDaemonInterface *daemonInterface = new OrgKdeKpilotDaemonInterface("org.kde.kpilot.daemon", "/Daemon", QDBusConnection::sessionBus());
00374 if (daemonInterface)
00375 {
00376 daemonInterface->startListening();
00377 }
00378 KPILOT_DELETE(daemonInterface);
00379 }
00380