• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • indi
indidriver.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  INDI Driver
3  -------------------
4  begin : Wed May 7th 2003
5  copyright : (C) 2001 by Jasem Mutlaq
6  email : mutlaqja@ikarustech.com
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "indidriver.h"
18 
19 #include <unistd.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 
24 #include <QRadioButton>
25 #include <QFile>
26 #include <QDir>
27 #include <QTextStream>
28 #include <QTreeWidget>
29 #include <QIcon>
30 #include <QDialog>
31 #include <QTcpServer>
32 
33 #include <KMenu>
34 #include <KMessageBox>
35 #include <KPushButton>
36 #include <KLineEdit>
37 #include <KProcess>
38 #include <KAction>
39 #include <KActionCollection>
40 #include <KIconLoader>
41 
42 #include <kstandarddirs.h>
43 
44 #include "oal/log.h"
45 #include "oal/scope.h"
46 #include "indimenu.h"
47 #include "ui_indihostconf.h"
48 #include "devicemanager.h"
49 #include "indidevice.h"
50 #include "Options.h"
51 
52 #include "kstars.h"
53 #include "kstarsdata.h"
54 #include "ksutils.h"
55 
56 #include <config-kstars.h>
57 
58 #define MAX_RETRIES 3
59 #define ERRMSG_SIZE 1024
60 
61 DeviceManagerUI::DeviceManagerUI(QWidget *parent) : QFrame(parent)
62 {
63 
64  setupUi(this);
65 
66  localTreeWidget->setSortingEnabled(false);
67  localTreeWidget->setRootIsDecorated(true);
68 
69  clientTreeWidget->setSortingEnabled(false);
70 
71  runningPix = KIcon( "system-run" );
72  stopPix = KIcon( "dialog-cancel" );
73  localMode = KIcon( "computer" );
74  serverMode = KIcon( "network-server" );
75 
76  connected = KIcon( "network-connect" );
77  disconnected = KIcon( "network-disconnect" );
78 
79  connect(localTreeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(makePortEditable(QTreeWidgetItem*,int)));
80 
81 }
82 
83 void DeviceManagerUI::makePortEditable(QTreeWidgetItem* selectedItem, int column)
84 {
85  // If it's the port column, then make it user-editable
86  if (column == INDIDriver::LOCAL_PORT_COLUMN)
87  selectedItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable|Qt::ItemIsEnabled);
88 
89  localTreeWidget->editItem(selectedItem, INDIDriver::LOCAL_PORT_COLUMN);
90 }
91 
92 INDIDriver::INDIDriver( KStars *_ks )
93  : KDialog( _ks ), ksw( _ks )
94 {
95 
96  currentPort = Options::serverPortStart().toInt()-1;
97  lastGroup = NULL;
98  lastDevice = NULL;
99 
100  ui = new DeviceManagerUI( this );
101  setMainWidget( ui );
102  setCaption( i18n( "Device Manager" ) );
103  setButtons( KDialog::Close );
104 
105  foreach ( INDIHostsInfo * host, ksw->data()->INDIHostsList )
106  {
107  QTreeWidgetItem *item = new QTreeWidgetItem(ui->clientTreeWidget, lastGroup);
108  lastGroup = item;
109  item->setIcon(HOST_STATUS_COLUMN, ui->disconnected);
110  item->setText(HOST_NAME_COLUMN, host->name);
111  item->setText(HOST_PORT_COLUMN, host->portnumber);
112  }
113 
114  lastGroup = NULL;
115 
116  QObject::connect(ui->addB, SIGNAL(clicked()), this, SLOT(addINDIHost()));
117  QObject::connect(ui->modifyB, SIGNAL(clicked()), this, SLOT(modifyINDIHost()));
118  QObject::connect(ui->removeB, SIGNAL(clicked()), this, SLOT(removeINDIHost()));
119 
120 
121  //QObject::connect(ksw->indiMenu(), SIGNAL(driverDisconnected(int)), this, SLOT(shutdownHost(int)));
122  QObject::connect(ui->connectHostB, SIGNAL(clicked()), this, SLOT(activateHostConnection()));
123  QObject::connect(ui->disconnectHostB, SIGNAL(clicked()), this, SLOT(activateHostDisconnection()));
124  QObject::connect(ui->runServiceB, SIGNAL(clicked()), this, SLOT(activateRunService()));
125  QObject::connect(ui->stopServiceB, SIGNAL(clicked()), this, SLOT(activateStopService()));
126  QObject::connect(ui->localTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(updateLocalTab()));
127  QObject::connect(ui->clientTreeWidget, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this, SLOT(updateClientTab()));
128  QObject::connect(ui->localTreeWidget, SIGNAL(expanded(const QModelIndex &)), this, SLOT(resizeDeviceColumn()));
129 
130  readXMLDrivers();
131 }
132 
133 /*
134 void INDIDriver::enableDevice(INDI_D *indi_device)
135 {
136  if (indi_device == NULL)
137  return;
138 
139  if (indi_device->deviceManager->mode == DeviceManager::M_CLIENT)
140  {
141  foreach (INDIHostsInfo * host, ksw->data()->INDIHostsList)
142  {
143 
144  if (host->deviceManager == indi_device->deviceManager && host->isConnected == false)
145  {
146  foreach (QTreeWidgetItem *item, ui->clientTreeWidget->findItems(host->name, Qt::MatchExactly, 1))
147  item->setIcon(HOST_STATUS_COLUMN, ui->connected);
148 
149  host->isConnected = true;
150  updateClientTab();
151  updateMenuActions();
152  ksw->indiMenu()->show();
153  return;
154  }
155  }
156  }
157  else
158  {
159  foreach (IDevice *device, devices)
160  {
161  if (device->unique_label == indi_device->label)
162  {
163  foreach (QTreeWidgetItem *item, ui->localTreeWidget->findItems(device->tree_label, Qt::MatchExactly | Qt::MatchRecursive))
164  {
165  item->setIcon(LOCAL_STATUS_COLUMN, ui->runningPix);
166  item->setText(LOCAL_PORT_COLUMN, QString::number(indi_device->deviceManager->port));
167  }
168 
169  updateLocalTab();
170  updateMenuActions();
171  ksw->indiMenu()->show();
172  return;
173  }
174  }
175  }
176 
177 }
178 
179 void INDIDriver::disableDevice(INDI_D *indi_device)
180 {
181  if (indi_device == NULL)
182  return;
183 
184  if (indi_device->deviceManager->mode == DeviceManager::M_CLIENT)
185  {
186  foreach (INDIHostsInfo * host, ksw->data()->INDIHostsList)
187  {
188  if (host->deviceManager == indi_device->deviceManager && host->isConnected == true)
189  {
190  foreach (QTreeWidgetItem *item, ui->clientTreeWidget->findItems(host->name, Qt::MatchExactly,1))
191  item->setIcon(HOST_STATUS_COLUMN, ui->disconnected);
192 
193  host->deviceManager = NULL;
194  host->isConnected = false;
195 
196  updateClientTab();
197  return;
198  }
199  }
200  }
201  else
202  {
203  foreach (IDevice *device, devices)
204  {
205  if (device->unique_label == indi_device->label)
206  {
207  foreach (QTreeWidgetItem *item, ui->localTreeWidget->findItems(device->tree_label, Qt::MatchExactly | Qt::MatchRecursive))
208  {
209  item->setIcon(LOCAL_STATUS_COLUMN, ui->stopPix);
210  item->setIcon(LOCAL_MODE_COLUMN, QIcon());
211  item->setText(LOCAL_PORT_COLUMN, device->port);
212  }
213 
214  device->clear();
215  updateLocalTab();
216 
217  return;
218  }
219  }
220  }
221  }
222  */
223 void INDIDriver::activateRunService()
224  {
225  processLocalTree(IDevice::DEV_START);
226  }
227 
228 void INDIDriver::activateStopService()
229  {
230  processLocalTree(IDevice::DEV_TERMINATE);
231  }
232 
233 void INDIDriver::activateHostConnection()
234  {
235  processRemoteTree(IDevice::DEV_START);
236  }
237 
238 void INDIDriver::activateHostDisconnection()
239  {
240  processRemoteTree(IDevice::DEV_TERMINATE);
241  }
242 
243 void INDIDriver::updateLocalTab()
244  {
245 
246  if (ui->localTreeWidget->currentItem() == NULL)
247  return;
248 
249  foreach (IDevice *device, devices)
250  {
251 
252  if (ui->localTreeWidget->currentItem()->text(LOCAL_NAME_COLUMN) == device->tree_label)
253  {
254  ui->runServiceB->setEnabled(device->state == IDevice::DEV_TERMINATE);
255  ui->stopServiceB->setEnabled(device->state == IDevice::DEV_START);
256 
257  ui->serverLogText->clear();
258  ui->serverLogText->append(device->getServerBuffer());
259 
260  return;
261  }
262  }
263 }
264 
265 void INDIDriver::updateClientTab()
266 {
267  if (ui->clientTreeWidget->currentItem() == NULL)
268  return;
269 
270  foreach (INDIHostsInfo * host, ksw->data()->INDIHostsList)
271  {
272  if (ui->clientTreeWidget->currentItem()->text(HOST_NAME_COLUMN) == host->name && ui->clientTreeWidget->currentItem()->text(HOST_PORT_COLUMN) == host->portnumber)
273  {
274  ui->connectHostB->setEnabled(!host->isConnected);
275  ui->disconnectHostB->setEnabled(host->isConnected);
276  break;
277  }
278  }
279 
280 }
281 
282 void INDIDriver::processLocalTree(IDevice::DeviceStatus dev_request)
283 {
284  QList<IDevice *> processed_devices;
285  DeviceManager *deviceManager=NULL;
286 
287  int port=0;
288  bool portOK=false;
289 
290  foreach(QTreeWidgetItem *item, ui->localTreeWidget->selectedItems())
291  {
292  foreach (IDevice *device, devices)
293  {
294  //device->state = (dev_request == IDevice::DEV_TERMINATE) ? IDevice::DEV_START : IDevice::DEV_TERMINATE;
295  if (item->text(LOCAL_NAME_COLUMN) == device->tree_label && device->state != dev_request)
296  {
297  processed_devices.append(device);
298 
299  // N.B. If multiple devices are selected to run under one device manager
300  // then we select the port for the first device that has a valid port
301  // entry, the rest are ignored.
302  if (port == 0 && item->text(LOCAL_PORT_COLUMN).isEmpty() == false)
303  {
304  port = item->text(LOCAL_PORT_COLUMN).toInt(&portOK);
305  // If we encounter conversion error, we abort
306  if (portOK == false)
307  {
308  KMessageBox::error(0, i18n("Invalid port entry: %1", item->text(LOCAL_PORT_COLUMN)));
309  return;
310  }
311  }
312  }
313  }
314  }
315 
316  if (processed_devices.empty()) return;
317 
318  // Select random port within range is none specified.
319  port = getINDIPort(port);
320 
321  if (dev_request == IDevice::DEV_START)
322  {
323 
324  if (port <= 0)
325  {
326  KMessageBox::error(0, i18n("Cannot start INDI server: port error."));
327  return;
328  }
329 
330  //deviceManager = ksw->indiMenu()->startDeviceManager(processed_devices, ui->localR->isChecked() ? DeviceManager::M_LOCAL : DeviceManager::M_SERVER, ((uint) port));
331  deviceManager = ksw->indiMenu()->initDeviceManager("localhost", ((uint) port), ui->localR->isChecked() ? DeviceManager::M_LOCAL : DeviceManager::M_SERVER);
332 
333  if (deviceManager == NULL)
334  {
335  kWarning() << "Warning: device manager has not been established properly";
336  return;
337  }
338 
339  deviceManager->appendManagedDevices(processed_devices);
340  deviceManager->startServer();
341  connect(deviceManager, SIGNAL(newServerInput()), this, SLOT(updateLocalTab()));
342  }
343  else
344  ksw->indiMenu()->stopDeviceManager(processed_devices);
345 
346 }
347 
348 void INDIDriver::processRemoteTree(IDevice::DeviceStatus dev_request)
349 {
350  DeviceManager* deviceManager=NULL;
351  QTreeWidgetItem *currentItem = ui->clientTreeWidget->currentItem();
352  if (!currentItem) return;
353  bool toConnect = (dev_request == IDevice::DEV_START);
354 
355  foreach (INDIHostsInfo * host, ksw->data()->INDIHostsList)
356  {
357  if (currentItem->text(HOST_NAME_COLUMN) == host->name && currentItem->text(HOST_PORT_COLUMN) == host->portnumber)
358  {
359  // Nothing changed, return
360  if (host->isConnected == toConnect)
361  return;
362 
363  // connect to host
364  if (toConnect)
365  {
366  deviceManager = ksw->indiMenu()->initDeviceManager(host->hostname, host->portnumber.toUInt(), DeviceManager::M_CLIENT);
367 
368  if (deviceManager == NULL)
369  {
370  // msgbox after freeze
371  kWarning() << "Warning: device manager has not been established properly";
372  return;
373  }
374 
375  host->deviceManager = deviceManager;
376  deviceManager->connectToServer();
377  }
378  else
379  ksw->indiMenu()->removeDeviceManager(host->deviceManager);
380 
381  return;
382  }
383  }
384 }
385 
386 void INDIDriver::newTelescopeDiscovered()
387 {
388 
389  emit newTelescope();
390 
391 }
392 
393 void INDIDriver::newCCDDiscovered()
394 {
395 
396  emit newCCD();
397 }
398 
399 void INDIDriver::resizeDeviceColumn()
400 {
401  ui->localTreeWidget->resizeColumnToContents(0);
402 }
403 
404 void INDIDriver::updateMenuActions()
405 {
406  // We iterate over devices, we enable INDI Control Panel if we have any active device
407  // We enable capture image sequence if we have any imaging device
408 
409  QAction *tmpAction = NULL;
410  INDIMenu *devMenu = ksw->indiMenu();
411  bool activeDevice = false;
412  bool activeImaging = false;
413  INDI_P *imgProp = NULL;
414 
415  if (devMenu == NULL)
416  return;
417 
418  if (devMenu->managers.count() > 0)
419  activeDevice = true;
420 
421  foreach(DeviceManager *dev_managers, devMenu->managers)
422  {
423  foreach (INDI_D *device, dev_managers->indi_dev)
424  {
425 
426  imgProp = device->findProp("CCD_EXPOSURE");
427  if (imgProp && device->isOn())
428  {
429  activeImaging = true;
430  break;
431  }
432  }
433  }
434 
435  tmpAction = ksw->actionCollection()->action("capture_sequence");
436 
437  if (tmpAction != NULL)
438  tmpAction->setEnabled(activeImaging);
439 
440  tmpAction = ksw->actionCollection()->action("indi_cpl");
441  if (tmpAction != NULL)
442  tmpAction->setEnabled(activeDevice);
443 
444 }
445 
446 bool INDIDriver::isDeviceRunning(const QString &deviceLabel)
447 {
448  foreach(IDevice *dev, devices) {
449  if (deviceLabel == dev->tree_label)
450  return dev->state == IDevice::DEV_START;
451  }
452  return false;
453 }
454 
455 int INDIDriver::getINDIPort(int customPort)
456 {
457 
458  int lastPort = Options::serverPortEnd().toInt();;
459  bool success = false;
460  currentPort++;
461 
462  // recycle
463  if (currentPort > lastPort) currentPort = Options::serverPortStart().toInt();
464 
465  QTcpServer temp_server;
466 
467  if (customPort != 0)
468  {
469  success = temp_server.listen(QHostAddress::LocalHost, customPort);
470  if (success)
471  {
472  temp_server.close();
473  return customPort;
474  }
475  else
476  return -1;
477  }
478 
479  for(; currentPort <= lastPort; currentPort++)
480  {
481  success = temp_server.listen(QHostAddress::LocalHost, currentPort);
482  if(success)
483  {
484  temp_server.close();
485  return currentPort;
486  }
487  }
488  return -1;
489 }
490 
491 bool INDIDriver::readXMLDrivers()
492 {
493  QDir indiDir;
494  QString driverName;
495 
496  if (indiDir.cd(Options::indiDriversDir()) == false)
497  {
498  KMessageBox::error(0, i18n("Unable to find INDI Drivers directory: %1\nPlease make sure to set the correct path in KStars configuration", Options::indiDriversDir()));
499  return false;
500  }
501 
502  indiDir.setNameFilters(QStringList("*.xml"));
503  indiDir.setFilter(QDir::Files | QDir::NoSymLinks);
504  QFileInfoList list = indiDir.entryInfoList();
505 
506  foreach (QFileInfo fileInfo, list)
507  {
508  // libindi 0.7.1: Skip skeleton files
509  if (fileInfo.fileName().endsWith("_sk.xml"))
510  continue;
511 
512  if (fileInfo.fileName() == "drivers.xml")
513  {
514  // Let first attempt to load the local version of drivers.xml
515  driverName = KStandardDirs::locateLocal( "appdata", "drivers.xml");
516 
517  // If found, we continue, otherwise, we load the system file
518  if (driverName.isEmpty() == false && QFile(driverName).exists())
519  {
520  processXMLDriver(driverName);
521  continue;
522  }
523  }
524 
525  driverName = QString("%1/%2").arg(Options::indiDriversDir()).arg(fileInfo.fileName());
526  processXMLDriver(driverName);
527 
528  }
529 
530 return true;
531 
532 }
533 
534 void INDIDriver::processXMLDriver(QString & driverName)
535 {
536  QFile file(driverName);
537  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
538  {
539  KMessageBox::error(0, i18n("Failed to open INDI Driver file: %1", driverName));
540  return;
541  }
542 
543  char errmsg[ERRMSG_SIZE];
544  char c;
545  LilXML *xmlParser = newLilXML();
546  XMLEle *root = NULL;
547 
548  if (driverName.endsWith("drivers.xml"))
549  xmlSource = IDevice::PRIMARY_XML;
550  else
551  xmlSource = IDevice::THIRD_PARTY_XML;
552 
553  while ( file.getChar(&c))
554  {
555  root = readXMLEle(xmlParser, c, errmsg);
556 
557  if (root)
558  {
559  if (!buildDeviceGroup(root, errmsg))
560  prXMLEle(stderr, root, 0);
561 
562  delXMLEle(root);
563  }
564  else if (errmsg[0])
565  {
566  kDebug() << QString(errmsg) << endl;
567  delLilXML(xmlParser);
568  return;
569  }
570  }
571 
572  delLilXML(xmlParser);
573 
574 }
575 
576 bool INDIDriver::buildDeviceGroup(XMLEle *root, char errmsg[])
577 {
578 
579  XMLAtt *ap;
580  XMLEle *ep;
581  QString groupName;
582  QTreeWidgetItem *group;
583  int groupType = KSTARS_TELESCOPE;
584 
585  // avoid overflow
586  if (strlen(tagXMLEle(root)) > 1024)
587  return false;
588 
589  // Get device grouping name
590  ap = findXMLAtt(root, "group");
591 
592  if (!ap)
593  {
594  snprintf(errmsg, ERRMSG_SIZE, "Tag %.64s does not have a group attribute", tagXMLEle(root));
595  return false;
596  }
597 
598  groupName = valuXMLAtt(ap);
599 
600  if (groupName.indexOf("Telescopes") != -1)
601  groupType = KSTARS_TELESCOPE;
602  else if (groupName.indexOf("CCDs") != -1)
603  groupType = KSTARS_CCD;
604  else if (groupName.indexOf("Filter") != -1)
605  groupType = KSTARS_FILTER;
606  else if (groupName.indexOf("Video") != -1)
607  groupType = KSTARS_VIDEO;
608  else if (groupName.indexOf("Focusers") != -1)
609  groupType = KSTARS_FOCUSER;
610  else if (groupName.indexOf("Domes") != -1)
611  groupType = KSTARS_DOME;
612  else if (groupName.indexOf("Receivers") != -1)
613  groupType = KSTARS_RECEIVERS;
614  else if (groupName.indexOf("GPS") != -1)
615  groupType = KSTARS_GPS;
616 
617 #ifndef HAVE_CFITSIO_H
618  // We do not create these groups if we don't have CFITSIO support
619  if (groupType == KSTARS_CCD || groupType == KSTARS_VIDEO)
620  return true;
621 #endif
622 
623  // Find if the group already exists
624  QList<QTreeWidgetItem *> treeList = ui->localTreeWidget->findItems(groupName, Qt::MatchExactly);
625  if (!treeList.isEmpty())
626  group = treeList[0];
627  else
628  group = new QTreeWidgetItem(ui->localTreeWidget, lastGroup);
629 
630  group->setText(0, groupName);
631  lastGroup = group;
632 
633  for (ep = nextXMLEle(root, 1) ; ep != NULL ; ep = nextXMLEle(root, 0))
634  if (!buildDriverElement(ep, group, groupType, errmsg))
635  return false;
636 
637  return true;
638 }
639 
640 
641 bool INDIDriver::buildDriverElement(XMLEle *root, QTreeWidgetItem *DGroup, int groupType, char errmsg[])
642 {
643  XMLAtt *ap;
644  XMLEle *el;
645  IDevice *dv;
646  QString label;
647  QString driver;
648  QString version;
649  QString name;
650  QString port;
651  double focal_length (-1), aperture (-1);
652 
653 
654  ap = findXMLAtt(root, "label");
655  if (!ap)
656  {
657  snprintf(errmsg, ERRMSG_SIZE, "Tag %.64s does not have a label attribute", tagXMLEle(root));
658  return false;
659  }
660 
661  label = valuXMLAtt(ap);
662 
663  // Search for optional port attribute
664  ap = findXMLAtt(root, "port");
665  if (ap)
666  port = valuXMLAtt(ap);
667 
668  // Let's look for telescope-specific attributes: focal length and aperture
669  ap = findXMLAtt(root, "focal_length");
670  if (ap)
671  focal_length = QString(valuXMLAtt(ap)).toDouble();
672 
673  ap = findXMLAtt(root, "aperture");
674  if (ap)
675  aperture = QString(valuXMLAtt(ap)).toDouble();
676 
677  el = findXMLEle(root, "driver");
678 
679  if (!el)
680  return false;
681 
682  driver = pcdataXMLEle(el);
683 
684  ap = findXMLAtt(el, "name");
685  if (!ap)
686  {
687  snprintf(errmsg, ERRMSG_SIZE, "Tag %.64s does not have a name attribute", tagXMLEle(el));
688  return false;
689  }
690 
691  name = valuXMLAtt(ap);
692 
693  el = findXMLEle(root, "version");
694 
695  if (!el)
696  return false;
697 
698  version = pcdataXMLEle(el);
699 
700  QTreeWidgetItem *device = new QTreeWidgetItem(DGroup, lastDevice);
701 
702  device->setText(LOCAL_NAME_COLUMN, label);
703  device->setIcon(LOCAL_STATUS_COLUMN, ui->stopPix);
704  device->setText(LOCAL_VERSION_COLUMN, version);
705  device->setText(LOCAL_PORT_COLUMN, port);
706 
707  lastDevice = device;
708 
709  if ((xmlSource == IDevice::PRIMARY_XML) && driversList.contains(driver) == false)
710  driversList.insert(driver, name);
711 
712  dv = new IDevice(name, label, driver, version);
713  dv->type = groupType;
714  dv->xmlSource = xmlSource;
715  //connect(dv, SIGNAL(newServerInput()), this, SLOT(updateLocalTab()));
716  if (focal_length > 0)
717  dv->focal_length = focal_length;
718  if (aperture > 0)
719  dv->aperture = aperture;
720  dv->port = port;
721 
722  devices.append(dv);
723 
724  // SLOTS/SIGNAL, pop menu, indi server logic
725  return true;
726 }
727 
728 void INDIDriver::updateCustomDrivers()
729 {
730  QString label;
731  QString driver;
732  QString version;
733  QString name;
734  IDevice *dv;
735  QTreeWidgetItem *group, *widgetDev;
736  double focal_length (-1), aperture (-1);
737 
738  // Find if the group already exists
739  QList<QTreeWidgetItem *> treeList = ui->localTreeWidget->findItems("Telescopes", Qt::MatchExactly);
740  if (!treeList.isEmpty())
741  group = treeList[0];
742  else return;
743 
744 
745  // Find custom telescope to ADD
746  foreach(OAL::Scope *s, *(KStarsData::Instance()->logObject()->scopeList()))
747  {
748  label = s->vendor() + " " + s->model();
749 
750  if (s->driver() == i18n("None") || findDeviceByLabel(label))
751  continue;
752 
753  QHash<QString, QString>::const_iterator i = driversList.constFind(s->driver());
754  if (i != driversList.constEnd() && i.key() == s->driver())
755  name = i.value();
756  else
757  return;
758  focal_length = s->focalLength();
759  aperture = s->aperture();
760  driver = s->driver();
761  version = QString("1.0");
762 
763  QTreeWidgetItem *device = new QTreeWidgetItem(group, lastDevice);
764  device->setText(LOCAL_NAME_COLUMN, QString(label));
765  device->setIcon(LOCAL_STATUS_COLUMN, ui->stopPix);
766  device->setText(LOCAL_VERSION_COLUMN, QString(version));
767 
768  lastDevice = device;
769 
770  dv = new IDevice(name, label, driver, version);
771  dv->type = KSTARS_TELESCOPE;
772  dv->xmlSource = IDevice::EM_XML;
773  dv->focal_length = focal_length;
774  dv->aperture = aperture;
775  dv->id = s->id();
776  devices.append(dv);
777  }
778 
779  // Find custom telescope to REMOVE
780  foreach(IDevice *dev, devices)
781  {
782  // If it's from primary xml file or it is in a running state, continue.
783  if (dev->xmlSource != IDevice::EM_XML || dev->state == IDevice::DEV_START)
784  continue;
785 
786  // See if log object has the device, if not delete it
787  if (KStarsData::Instance()->logObject()->findScopeById(dev->id))
788  continue;
789 
790  // Find if the group already exists
791  QList<QTreeWidgetItem *> devList = ui->localTreeWidget->findItems(dev->tree_label, Qt::MatchExactly | Qt::MatchRecursive);
792  if (!devList.isEmpty())
793  {
794  widgetDev = devList[0];
795  group->removeChild(widgetDev);
796  }
797  else return;
798 
799  devices.removeOne(dev);
800  delete (dev);
801  }
802 
803 }
804 
805 void INDIDriver::addINDIHost()
806 {
807  QDialog hostConfDialog;
808  Ui::INDIHostConf hostConf;
809  hostConf.setupUi(&hostConfDialog);
810  hostConfDialog.setWindowTitle(i18n("Add Host"));
811  bool portOk = false;
812 
813  if (hostConfDialog.exec() == QDialog::Accepted)
814  {
815  INDIHostsInfo *hostItem = new INDIHostsInfo;
816  hostItem->name = hostConf.nameIN->text();
817  hostItem->hostname = hostConf.hostname->text();
818  hostItem->portnumber = hostConf.portnumber->text();
819  hostItem->isConnected = false;
820  hostItem->deviceManager = NULL;
821 
822  hostItem->portnumber.toInt(&portOk);
823 
824  if (portOk == false)
825  {
826  KMessageBox::error(0, i18n("Error: the port number is invalid."));
827  delete hostItem;
828  return;
829  }
830 
831  //search for duplicates
832  //for (uint i=0; i < ksw->data()->INDIHostsList.count(); i++)
833  foreach (INDIHostsInfo * host, ksw->data()->INDIHostsList)
834  if (hostItem->name == host->name && hostItem->portnumber == host->portnumber)
835  {
836  KMessageBox::error(0, i18n("Host: %1 Port: %2 already exists.", hostItem->name, hostItem->portnumber));
837  delete hostItem;
838  return;
839  }
840 
841  ksw->data()->INDIHostsList.append(hostItem);
842 
843  QTreeWidgetItem *item = new QTreeWidgetItem(ui->clientTreeWidget);
844  item->setIcon(HOST_STATUS_COLUMN, ui->disconnected);
845  item->setText(HOST_NAME_COLUMN, hostConf.nameIN->text());
846  item->setText(HOST_PORT_COLUMN, hostConf.portnumber->text());
847 
848  }
849 
850  saveHosts();
851 }
852 
853 
854 
855 void INDIDriver::modifyINDIHost()
856 {
857 
858  QDialog hostConfDialog;
859  Ui::INDIHostConf hostConf;
860  hostConf.setupUi(&hostConfDialog);
861  hostConfDialog.setWindowTitle(i18n("Modify Host"));
862 
863  QTreeWidgetItem *currentItem = ui->clientTreeWidget->currentItem();
864 
865  if (currentItem == NULL)
866  return;
867 
868  foreach (INDIHostsInfo * host, ksw->data()->INDIHostsList)
869  {
870  if (currentItem->text(HOST_NAME_COLUMN) == host->name && currentItem->text(HOST_PORT_COLUMN) == host->portnumber)
871  {
872  hostConf.nameIN->setText(host->name);
873  hostConf.hostname->setText(host->hostname);
874  hostConf.portnumber->setText(host->portnumber);
875 
876  if (hostConfDialog.exec() == QDialog::Accepted)
877  {
878  //INDIHostsInfo *hostItem = new INDIHostsInfo;
879  host->name = hostConf.nameIN->text();
880  host->hostname = hostConf.hostname->text();
881  host->portnumber = hostConf.portnumber->text();
882 
883  currentItem->setText(HOST_NAME_COLUMN, hostConf.nameIN->text());
884  currentItem->setText(HOST_PORT_COLUMN, hostConf.portnumber->text());
885 
886  //ksw->data()->INDIHostsList.replace(i, hostItem);
887 
888  saveHosts();
889  return;
890  }
891  }
892  }
893 
894 }
895 
896 void INDIDriver::removeINDIHost()
897 {
898 
899  if (ui->clientTreeWidget->currentItem() == NULL)
900  return;
901 
902  for (int i=0; i < ksw->data()->INDIHostsList.count(); i++)
903  if (ui->clientTreeWidget->currentItem()->text(HOST_NAME_COLUMN) == ksw->data()->INDIHostsList[i]->name &&
904  ui->clientTreeWidget->currentItem()->text(HOST_PORT_COLUMN) == ksw->data()->INDIHostsList[i]->portnumber)
905  {
906  if (ksw->data()->INDIHostsList[i]->isConnected)
907  {
908  KMessageBox::error( 0, i18n("You need to disconnect the client before removing it."));
909  return;
910  }
911 
912  if (KMessageBox::warningContinueCancel( 0, i18n("Are you sure you want to remove the %1 client?", ui->clientTreeWidget->currentItem()->text(HOST_NAME_COLUMN)), i18n("Delete Confirmation"),KStandardGuiItem::del())!=KMessageBox::Continue)
913  return;
914 
915  delete ksw->data()->INDIHostsList.takeAt(i);
916  delete (ui->clientTreeWidget->currentItem());
917  break;
918  }
919 
920 
921 
922  saveHosts();
923 }
924 
925 void INDIDriver::saveHosts()
926 {
927 
928  QFile file;
929  QString hostData;
930 
931  file.setFileName( KStandardDirs::locateLocal( "appdata", "indihosts.xml" ) ); //determine filename in local user KDE directory tree.
932 
933  if ( !file.open( QIODevice::WriteOnly))
934  {
935  QString message = i18n( "Unable to write to file 'indihosts.xml'\nAny changes to INDI hosts configurations will not be saved." );
936  KMessageBox::sorry( 0, message, i18n( "Could Not Open File" ) );
937  return;
938  }
939 
940  QTextStream outstream(&file);
941 
942  //for (uint i= 0; i < ksw->data()->INDIHostsList.count(); i++)
943  foreach (INDIHostsInfo * host, ksw->data()->INDIHostsList)
944  {
945 
946  hostData = "<INDIHost name='";
947  hostData += host->name;
948  hostData += "' hostname='";
949  hostData += host->hostname;
950  hostData += "' port='";
951  hostData += host->portnumber;
952  hostData += "' />\n";
953 
954  outstream << hostData;
955 
956  }
957 
958  file.close();
959 
960 }
961 
962 IDevice * INDIDriver::findDeviceByLabel(const QString &label)
963 {
964  foreach(IDevice *dev, devices)
965  if (dev->tree_label == label)
966  return dev;
967 
968  return NULL;
969 }
970 
971 INDIDriver::~INDIDriver()
972 {
973 
974  //for (uint i=0; i < devices.size(); i++)
975  //delete (devices[i]);
976  while (!devices.isEmpty())
977  delete devices.takeFirst();
978 
979 }
980 
981 IDevice::IDevice(const QString &inName, const QString &inLabel, const QString &inDriver, const QString &inVersion)
982 {
983  tree_label = inLabel;
984  unique_label.clear();
985  name = inName;
986  driver = inDriver;
987  version = inVersion;
988 
989  xmlSource = PRIMARY_XML;
990 
991  // Initially off
992  state = IDevice::DEV_TERMINATE;
993 
994  // No port initially
995  //indiPort = -1;
996 
997  // not yet managed by DeviceManager
998  //managed = false;
999 
1000  deviceManager = NULL;
1001 
1002  focal_length = -1;
1003  aperture = -1;
1004 
1005  //proc = NULL;
1006 
1007 }
1008 
1009 IDevice::~IDevice()
1010 {
1011 
1012 }
1013 
1014 void IDevice::clear()
1015 {
1016  //managed = false;
1017  state = IDevice::DEV_TERMINATE;
1018  deviceManager = NULL;
1019 
1020  unique_label.clear();
1021 }
1022 
1023 QString IDevice::getServerBuffer()
1024 {
1025  if (deviceManager != NULL)
1026  return deviceManager->getServerBuffer();
1027 
1028  return QString();
1029 }
1030 
1031 #include "indidriver.moc"
devicemanager.h
INDI_P
Definition: indiproperty.h:38
INDIHostsInfo::portnumber
QString portnumber
Definition: indidriver.h:41
Options::serverPortStart
static QString serverPortStart()
Get INDI Server Start Port.
Definition: Options.h:714
DeviceManagerUI::runningPix
QIcon runningPix
Definition: indidriver.h:86
INDIDriver::ui
DeviceManagerUI * ui
Definition: indidriver.h:117
IDevice::id
QString id
Definition: indidriver.h:62
INDIDriver::newTelescopeDiscovered
void newTelescopeDiscovered()
Definition: indidriver.cpp:386
INDIDriver::activateHostConnection
void activateHostConnection()
Definition: indidriver.cpp:233
IDevice::DEV_START
Definition: indidriver.h:54
indimenu.h
INDIDriver::modifyINDIHost
void modifyINDIHost()
Definition: indidriver.cpp:855
DeviceManager::connectToServer
void connectToServer()
Definition: devicemanager.cpp:127
INDIDriver::removeINDIHost
void removeINDIHost()
Definition: indidriver.cpp:896
INDIDriver::updateLocalTab
void updateLocalTab()
Definition: indidriver.cpp:243
INDIDriver::activateStopService
void activateStopService()
Definition: indidriver.cpp:228
QDialog
INDIDriver::buildDeviceGroup
bool buildDeviceGroup(XMLEle *root, char errmsg[])
Definition: indidriver.cpp:576
OAL::Scope
Definition: scope.h:25
DeviceManagerUI::makePortEditable
void makePortEditable(QTreeWidgetItem *selectedItem, int column)
Definition: indidriver.cpp:83
INDIDriver::activateHostDisconnection
void activateHostDisconnection()
Definition: indidriver.cpp:238
IDevice::DeviceStatus
DeviceStatus
Definition: indidriver.h:54
INDIDriver::resizeDeviceColumn
void resizeDeviceColumn()
Definition: indidriver.cpp:399
INDIDriver::getINDIPort
int getINDIPort(int customPort)
Definition: indidriver.cpp:455
QWidget
KStarsData::Instance
static KStarsData * Instance()
Definition: kstarsdata.h:92
IDevice::DEV_TERMINATE
Definition: indidriver.h:54
INDIHostsInfo::name
QString name
Definition: indidriver.h:39
DeviceManager::getServerBuffer
QString getServerBuffer()
Definition: devicemanager.h:81
INDIDriver::LOCAL_STATUS_COLUMN
Definition: indidriver.h:105
KDialog
KSTARS_DOME
Definition: indicommon.h:66
indidevice.h
INDIDriver::addINDIHost
void addINDIHost()
Definition: indidriver.cpp:805
INDIDriver::devices
QList< IDevice * > devices
Definition: indidriver.h:118
IDevice::tree_label
QString tree_label
Definition: indidriver.h:57
IDevice::name
QString name
Definition: indidriver.h:59
DeviceManagerUI::localMode
QIcon localMode
Definition: indidriver.h:90
KStars
This is the main window for KStars.
Definition: kstars.h:94
KSTARS_FOCUSER
Definition: indicommon.h:66
OAL::Scope::aperture
double aperture() const
Definition: scope.h:35
INDIHostsInfo::isConnected
bool isConnected
Definition: indidriver.h:42
INDIMenu
Definition: indimenu.h:37
INDIDriver::findDeviceByLabel
IDevice * findDeviceByLabel(const QString &label)
Definition: indidriver.cpp:962
INDIHostsInfo
Definition: indidriver.h:37
IDevice::type
int type
Definition: indidriver.h:68
INDIDriver::INDIDriver
INDIDriver(KStars *ks)
Definition: indidriver.cpp:92
INDIDriver::buildDriverElement
bool buildDriverElement(XMLEle *root, QTreeWidgetItem *DGroup, int groupType, char errmsg[])
Definition: indidriver.cpp:641
INDIDriver::processRemoteTree
void processRemoteTree(IDevice::DeviceStatus dev_request)
Definition: indidriver.cpp:348
DeviceManager::appendManagedDevices
void appendManagedDevices(QList< IDevice * > &processed_devices)
Definition: devicemanager.cpp:192
DeviceManager::M_SERVER
Definition: devicemanager.h:31
DeviceManagerUI
Definition: indidriver.h:79
KSTARS_RECEIVERS
Definition: indicommon.h:66
INDIDriver::LOCAL_NAME_COLUMN
Definition: indidriver.h:105
IDevice::clear
void clear()
Definition: indidriver.cpp:1014
INDIDriver::HOST_NAME_COLUMN
Definition: indidriver.h:106
INDIDriver::saveHosts
void saveHosts()
Definition: indidriver.cpp:925
DeviceManagerUI::stopPix
QIcon stopPix
Definition: indidriver.h:87
INDIDriver::isDeviceRunning
bool isDeviceRunning(const QString &deviceLabel)
Definition: indidriver.cpp:446
INDIDriver::currentPort
int currentPort
Definition: indidriver.h:123
DeviceManager::startServer
void startServer()
Definition: devicemanager.cpp:79
INDIDriver::LOCAL_VERSION_COLUMN
Definition: indidriver.h:105
IDevice::THIRD_PARTY_XML
Definition: indidriver.h:55
IDevice::getServerBuffer
QString getServerBuffer()
Definition: indidriver.cpp:1023
OAL::Scope::id
QString id() const
Definition: scope.h:28
INDIDriver::lastDevice
QTreeWidgetItem * lastDevice
Definition: indidriver.h:120
INDI_D
Definition: indidevice.h:30
INDIDriver::HOST_PORT_COLUMN
Definition: indidriver.h:106
INDIHostsInfo::deviceManager
DeviceManager * deviceManager
Definition: indidriver.h:43
DeviceManagerUI::DeviceManagerUI
DeviceManagerUI(QWidget *parent=0)
Definition: indidriver.cpp:61
IDevice::driver
QString driver
Definition: indidriver.h:60
KSTARS_GPS
Definition: indicommon.h:66
Options::indiDriversDir
static QString indiDriversDir()
Get PATH to indi drivers directory.
Definition: Options.h:790
INDIDriver::updateCustomDrivers
void updateCustomDrivers()
Definition: indidriver.cpp:728
scope.h
IDevice::focal_length
double focal_length
Definition: indidriver.h:71
INDIHostsInfo::hostname
QString hostname
Definition: indidriver.h:40
INDIDriver::newTelescope
void newTelescope()
OAL::Scope::focalLength
double focalLength() const
Definition: scope.h:34
Options::serverPortEnd
static QString serverPortEnd()
Get INDI Server Final Port.
Definition: Options.h:733
IDevice::EM_XML
Definition: indidriver.h:55
INDIDriver::updateClientTab
void updateClientTab()
Definition: indidriver.cpp:265
INDIDriver::HOST_STATUS_COLUMN
Definition: indidriver.h:106
IDevice::version
QString version
Definition: indidriver.h:61
indidriver.h
IDevice::state
DeviceStatus state
Definition: indidriver.h:64
KSTARS_CCD
Definition: indicommon.h:66
DeviceManagerUI::connected
QIcon connected
Definition: indidriver.h:88
INDIDriver::driversList
QHash< QString, QString > driversList
Definition: indidriver.h:122
Options.h
IDevice::IDevice
IDevice(const QString &inName, const QString &inLabel, const QString &inDriver, const QString &inVersion)
Definition: indidriver.cpp:981
INDIDriver::processXMLDriver
void processXMLDriver(QString &driverName)
Definition: indidriver.cpp:534
QTextStream
KSTARS_VIDEO
Definition: indicommon.h:66
DeviceManager::M_CLIENT
Definition: devicemanager.h:31
DeviceManagerUI::disconnected
QIcon disconnected
Definition: indidriver.h:89
INDIDriver::LOCAL_PORT_COLUMN
Definition: indidriver.h:105
IDevice::port
QString port
Definition: indidriver.h:63
OAL::Scope::driver
QString driver() const
Definition: scope.h:33
IDevice::~IDevice
~IDevice()
Definition: indidriver.cpp:1009
INDIDriver::newCCD
void newCCD()
IDevice
Definition: indidriver.h:46
IDevice::aperture
double aperture
Definition: indidriver.h:72
IDevice::xmlSource
XMLSource xmlSource
Definition: indidriver.h:65
KSTARS_FILTER
Definition: indicommon.h:66
INDIDriver::processLocalTree
void processLocalTree(IDevice::DeviceStatus dev_request)
Definition: indidriver.cpp:282
INDIDriver::activateRunService
void activateRunService()
Definition: indidriver.cpp:223
INDIMenu::managers
QList< DeviceManager * > managers
Definition: indimenu.h:46
INDIDriver::newCCDDiscovered
void newCCDDiscovered()
Definition: indidriver.cpp:393
KStars::data
KStarsData * data() const
Definition: kstars.h:131
INDIDriver::~INDIDriver
~INDIDriver()
Definition: indidriver.cpp:971
IDevice::unique_label
QString unique_label
Definition: indidriver.h:58
DeviceManager::M_LOCAL
Definition: devicemanager.h:31
ERRMSG_SIZE
#define ERRMSG_SIZE
Definition: indidriver.cpp:59
IDevice::deviceManager
DeviceManager * deviceManager
Definition: indidriver.h:67
IDevice::PRIMARY_XML
Definition: indidriver.h:55
kstarsdata.h
INDIDriver::lastGroup
QTreeWidgetItem * lastGroup
Definition: indidriver.h:119
DeviceManager::indi_dev
QList< INDI_D * > indi_dev
Definition: devicemanager.h:38
OAL::Scope::model
QString model() const
Definition: scope.h:29
INDIDriver::readXMLDrivers
bool readXMLDrivers()
Definition: indidriver.cpp:491
KSTARS_TELESCOPE
Definition: indicommon.h:66
INDIDriver::ksw
KStars * ksw
Definition: indidriver.h:116
log.h
DeviceManager
Definition: devicemanager.h:27
ksutils.h
QFrame
INDIDriver::xmlSource
IDevice::XMLSource xmlSource
Definition: indidriver.h:124
DeviceManagerUI::serverMode
QIcon serverMode
Definition: indidriver.h:91
kstars.h
OAL::Scope::vendor
QString vendor() const
Definition: scope.h:30
INDIDriver::updateMenuActions
void updateMenuActions()
Definition: indidriver.cpp:404
QList
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal