optionsWidget.cc 18.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
Jan Möbius's avatar
Jan Möbius committed
15
//
16 17 18 19
//  OpenFlipper 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 Lesser General Public License for more details.
Jan Möbius's avatar
Jan Möbius committed
20
//
21 22 23 24 25
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
Jan Möbius's avatar
Jan Möbius committed
26 27 28
//   $Revision$
//   $Author$
//   $Date$
29 30 31 32 33 34 35 36 37
//
//=============================================================================




#include "optionsWidget.hh"
#include <iostream>
#include <OpenFlipper/common/GlobalOptions.hh>
Dirk Wilden's avatar
Dirk Wilden committed
38 39
#include <ACG/Scenegraph/DrawModes.hh>
#include <OpenFlipper/ACGHelper/DrawModeConverter.hh>
40
#include <OpenFlipper/INIFile/INIFile.hh>
Dirk Wilden's avatar
Dirk Wilden committed
41

42 43
#include <QColorDialog>

Dirk Wilden's avatar
Dirk Wilden committed
44
OptionsWidget::OptionsWidget(std::vector<PluginInfo>& _plugins, std::vector<KeyBinding>& _core, InverseKeyMap& _invKeys, QWidget *parent)
Jan Möbius's avatar
Jan Möbius committed
45 46
  : QWidget(parent),
    plugins_(_plugins),
Dirk Wilden's avatar
Dirk Wilden committed
47 48
    coreKeys_(_core),
    keys_(_invKeys)
Jan Möbius's avatar
Jan Möbius committed
49

50 51
{
  setupUi(this);
Dirk Wilden's avatar
Dirk Wilden committed
52

Dirk Wilden's avatar
Dirk Wilden committed
53
  connect(okButton,SIGNAL(clicked()),this,SLOT(slotOk()));
54 55
  connect(applyButton,SIGNAL(clicked()),this,SLOT(slotApply()));
  connect(cancelButton,SIGNAL(clicked()),this,SLOT(slotCancel()));
Jan Möbius's avatar
Jan Möbius committed
56
  connect(checkUpdateButton,SIGNAL(clicked()),this,SLOT(slotCheckUpdates()));
Jan Möbius's avatar
Jan Möbius committed
57 58
  connect(updateButton,SIGNAL(clicked()),this,SLOT(slotGetUpdates()));
  updateButton->setEnabled(false);
Dirk Wilden's avatar
Dirk Wilden committed
59
  connect( restrictFPS, SIGNAL(toggled(bool)), FPS, SLOT(setEnabled(bool)) );
Dirk Wilden's avatar
Dirk Wilden committed
60 61
  uint mode = 2;
  for (uint i=1; i < 22; i++) {
62
    std::vector< QString > dm = drawModeToDescriptions( mode );
Jan Möbius's avatar
Jan Möbius committed
63

64
    if ( !dm.empty() && dm[0].trimmed() != ""){
Dirk Wilden's avatar
Dirk Wilden committed
65
      QListWidgetItem* item = new QListWidgetItem(dm[0]);
Jan Möbius's avatar
Jan Möbius committed
66

Dirk Wilden's avatar
Dirk Wilden committed
67
      item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
68 69 70 71 72

//       if (OpenFlipper::Options::standardDrawMode() )
//         item->setCheckState(Qt::Checked);
//       else
        item->setCheckState(Qt::Unchecked);
Dirk Wilden's avatar
Dirk Wilden committed
73 74 75

      availDrawModes->addItem( item );
    }
76

Dirk Wilden's avatar
Dirk Wilden committed
77 78
    mode = mode<<1;

79
  }
Jan Möbius's avatar
Jan Möbius committed
80

Dirk Wilden's avatar
Dirk Wilden committed
81 82 83
  pluginOptionsLayout = new QVBoxLayout;
  pluginOptions->setLayout( pluginOptionsLayout );

Jan Möbius's avatar
Jan Möbius committed
84
  http = new QHttp(this);
Jan Möbius's avatar
Jan Möbius committed
85

Jan Möbius's avatar
Jan Möbius committed
86 87 88 89 90 91 92
  // http specific connections
  connect(http, SIGNAL(requestFinished(int, bool)),
        this, SLOT(httpRequestFinished(int, bool)));
  connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
        this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
  connect(http, SIGNAL(dataReadProgress(int, int)),
          this, SLOT(updateDataReadProgress(int, int)));
Jan Möbius's avatar
Jan Möbius committed
93

Jan Möbius's avatar
Jan Möbius committed
94 95 96

  progressDialog = new QProgressDialog(this);
  connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
97 98 99 100


  //colordialog
  connect(backgroundButton, SIGNAL(clicked()), this, SLOT(getBackgroundColor()) );
101
  connect(baseColorButton, SIGNAL(clicked()), this, SLOT(getBaseColor()) );
102 103 104
}

void OptionsWidget::getBackgroundColor(){
105
  QColor newColor = QColorDialog::getColor ( OpenFlipper::Options::defaultBackgroundColor() );
Jan Möbius's avatar
Jan Möbius committed
106

107
  OpenFlipper::Options::defaultBackgroundColor( newColor) ;
108 109

  QPixmap color(16,16);
110
  color.fill( OpenFlipper::Options::defaultBackgroundColor() );
111
  backgroundButton->setIcon( QIcon(color) );
112 113
}

114 115 116 117 118 119 120 121 122 123
void OptionsWidget::getBaseColor(){
  QColor newColor = QColorDialog::getColor ( OpenFlipper::Options::defaultBaseColor() );

  OpenFlipper::Options::defaultBaseColor( newColor) ;

  QPixmap color(16,16);
  color.fill( OpenFlipper::Options::defaultBaseColor() );
  baseColorButton->setIcon( QIcon(color) );
}

Dirk Wilden's avatar
Dirk Wilden committed
124
void OptionsWidget::showEvent ( QShowEvent * /*event*/ ) {
Dirk Wilden's avatar
Dirk Wilden committed
125

Jan Möbius's avatar
Jan Möbius committed
126
  //general
Dirk Wilden's avatar
Dirk Wilden committed
127 128 129
  fullscreen->setChecked( OpenFlipper::Options::fullScreen() );
  splash->setChecked( OpenFlipper::Options::splash() );
  toolbox->setChecked( !OpenFlipper::Options::hideToolbox() );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
130 131 132 133

  loggerHidden->setChecked( OpenFlipper::Options::loggerState() == OpenFlipper::Options::Hidden);
  loggerNormal->setChecked( OpenFlipper::Options::loggerState() == OpenFlipper::Options::Normal);
  loggerInScene->setChecked( OpenFlipper::Options::loggerState() == OpenFlipper::Options::InScene);
Dirk Wilden's avatar
Dirk Wilden committed
134
  enableLogFile->setChecked( OpenFlipper::Options::logFileEnabled() );
Jan Möbius's avatar
Jan Möbius committed
135

Dirk Wilden's avatar
Dirk Wilden committed
136 137
  //paths
  logFile->setText( OpenFlipper::Options::logFile() );
Dirk Wilden's avatar
Dirk Wilden committed
138 139 140 141 142

  //viewer
  wZoomFactor->setText( QString::number(OpenFlipper::Options::wheelZoomFactor(), 'f') );
  wZoomFactorShift->setText( QString::number(OpenFlipper::Options::wheelZoomFactorShift(), 'f') );

143 144 145
  restrictFPS->setChecked( OpenFlipper::Options::restrictFrameRate() );
  FPS->setValue( OpenFlipper::Options::maxFrameRate() );

146
  QPixmap color(16,16);
147
  color.fill( OpenFlipper::Options::defaultBackgroundColor() );
148 149
  backgroundButton->setIcon( QIcon(color) );

150 151 152 153 154
  color.fill( OpenFlipper::Options::defaultBaseColor() );
  baseColorButton->setIcon( QIcon(color) );

  randomBaseColor->setChecked( OpenFlipper::Options::randomBaseColor() );

Dirk Wilden's avatar
Dirk Wilden committed
155 156 157
  // plugin options
  initPluginOptions();

158 159 160 161 162
  // updates
  updateUser->setText( OpenFlipper::Options::updateUsername() );
  updatePass->setText( OpenFlipper::Options::updatePassword() );
  updateURL->setText( OpenFlipper::Options::updateUrl( ) );

Jan Möbius's avatar
Jan Möbius committed
163 164 165
  // debugging
  slotDebugging->setChecked(OpenFlipper::Options::doSlotDebugging());

Ellen Dekkers's avatar
Ellen Dekkers committed
166
  renderPicking->setChecked(OpenFlipper::Options::renderPicking());
167 168 169
  int itemIndex = pickingRenderMode->findText( OpenFlipper::Options::pickingRenderMode() );
  if (  itemIndex != -1 )
    pickingRenderMode->setCurrentIndex(itemIndex);
170

Dirk Wilden's avatar
Dirk Wilden committed
171
  //keyBindings
Dirk Wilden's avatar
Dirk Wilden committed
172
  initKeyTree();
173

Dirk Wilden's avatar
Dirk Wilden committed
174
  keyTree->disconnect(); //to be sure..disconnect every slot
Dirk Wilden's avatar
Dirk Wilden committed
175

Dirk Wilden's avatar
Dirk Wilden committed
176 177
  connect(keyTree, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
          this,    SLOT(keyTreeItemChanged( QTreeWidgetItem*, QTreeWidgetItem*)) );
Dirk Wilden's avatar
Dirk Wilden committed
178

Dirk Wilden's avatar
Dirk Wilden committed
179 180
  connect(keyTree, SIGNAL(itemDoubleClicked (QTreeWidgetItem*, int)),
          this,    SLOT(keyTreeDoubleClicked( QTreeWidgetItem*, int)) );
Dirk Wilden's avatar
Dirk Wilden committed
181 182


Dirk Wilden's avatar
Dirk Wilden committed
183
  connect(shortcutButton, SIGNAL(keyChanged()), this, SLOT(updateShortcut()) );
184
  connect(presetsButton, SIGNAL(clicked()), this, SLOT(restoreKeyPresets()) );
185 186 187 188 189

  for (int i = 0 ; i < availDrawModes->count(); ++i )
    availDrawModes->item( i )->setCheckState(Qt::Unchecked) ;

  //Check the drawModes from StandardDrawMode
Dirk Wilden's avatar
Dirk Wilden committed
190
  std::vector< QString > dm = drawModeToDescriptions( OpenFlipper::Options::standardDrawMode() );
191 192 193 194 195 196 197 198

  for (uint i=0; i < dm.size(); i++){
    if ( !dm.empty() ) {
      QList< QListWidgetItem* > found = availDrawModes->findItems(dm[i],Qt::MatchExactly);
      for(int k=0; k < found.count(); k++)
        (found[k])->setCheckState(Qt::Checked);
    }
  }
Jan Möbius's avatar
Jan Möbius committed
199 200 201 202 203 204 205

  updateVersionsTable();

}

void OptionsWidget::updateVersionsTable() {

Jan Möbius's avatar
Jan Möbius committed
206 207
  updatedPlugins_.clear();

Jan Möbius's avatar
Jan Möbius committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  QString fileName = QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
                     ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + "Versions.ini" ;

  INIFile ini;

  ini.connect(fileName,false);

  QString systemString = "";

  if ( OpenFlipper::Options::isWindows() ) {
    systemString = "VersionWindows";
  } else if (OpenFlipper::Options::isLinux()) {
    systemString = "VersionLinux";
  } else {
    std::cerr << "Unsupported platform for update" << std::endl;
    return;
  }

  updateList->clear();
  updateList->setRowCount( 1 + plugins_.size() );
  updateList->setColumnCount(3);

  QStringList header;

  header << "Component" << "current Version" << "latest Version" ;
  updateList->setHorizontalHeaderLabels(header);

  QBrush currentBrush(Qt::white);

  QTableWidgetItem * newItem = 0;

  QString coreVersion;

  if ( ini.is_connected() && ini.get_entry(coreVersion, "Core" , systemString ) ) {

    // Newer Version available
Jan Möbius's avatar
Jan Möbius committed
244
    if ( isNewer(OpenFlipper::Options::coreVersion(),coreVersion) ) {
Jan Möbius's avatar
Jan Möbius committed
245
      currentBrush.setColor(Qt::red);
Jan Möbius's avatar
Jan Möbius committed
246 247
      updatedPlugins_ << "Core";
    } else if ( isNewer(coreVersion , OpenFlipper::Options::coreVersion()) )
248
      currentBrush.setColor(Qt::blue);
Jan Möbius's avatar
Jan Möbius committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    else
      currentBrush.setColor(Qt::green);

    newItem = new QTableWidgetItem( coreVersion );

  } else {

    // No local Information available
    currentBrush.setColor(Qt::yellow);

    newItem = new QTableWidgetItem( "Not Available" );
  }

  newItem->setBackground(currentBrush);
  updateList->setItem(0, 2, newItem);

  newItem = new QTableWidgetItem( OpenFlipper::Options::coreVersion() );
  newItem->setBackground(currentBrush);
  updateList->setItem(0, 1, newItem);

  newItem = new QTableWidgetItem( "Core" );
  newItem->setBackground(currentBrush);
  updateList->setItem(0, 0, newItem);


  for ( uint i = 0 ; i < plugins_.size(); ++i ) {
    QString latestVersion;

    if ( ini.is_connected() && ini.get_entry(latestVersion, plugins_[i].name , systemString ) ) {


      // Newer Version available
Jan Möbius's avatar
Jan Möbius committed
281
      if ( isNewer(plugins_[i].version,latestVersion) ) {
282
         currentBrush.setColor(Qt::red);
Jan Möbius's avatar
Jan Möbius committed
283 284 285
         QFileInfo pluginFile(plugins_[i].path );
         updatedPlugins_ << pluginFile.fileName();
      } else if ( isNewer(latestVersion,plugins_[i].version) )
286
         currentBrush.setColor(Qt::blue);
Jan Möbius's avatar
Jan Möbius committed
287
      else
288
         currentBrush.setColor(Qt::green);
Jan Möbius's avatar
Jan Möbius committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

      newItem = new QTableWidgetItem( latestVersion );

    } else {

      // No local Information available
      currentBrush.setColor(Qt::yellow);

      newItem = new QTableWidgetItem( "Not Available" );
    }

    newItem->setBackground(currentBrush);
    updateList->setItem( i + 1 , 2, newItem);

    newItem = new QTableWidgetItem( plugins_[i].version );
    newItem->setBackground(currentBrush);
    updateList->setItem( i + 1 , 1, newItem);

    newItem = new QTableWidgetItem( plugins_[i].name );
    newItem->setBackground(currentBrush);
    updateList->setItem( i + 1 , 0, newItem);
  }

  updateList->resizeColumnsToContents();

Jan Möbius's avatar
Jan Möbius committed
314 315
  updateButton->setEnabled(!updatedPlugins_.empty());

316 317
}

Dirk Wilden's avatar
Dirk Wilden committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
void OptionsWidget::initPluginOptions(){

  pluginList->disconnect();

  connect(pluginList, SIGNAL( currentTextChanged(const QString&) ), this, SLOT( slotShowPluginOptions(const QString&) ) );

  //init list of plugins
  pluginList->clear();

  for ( uint i = 0 ; i < plugins_.size(); ++i )
    if (plugins_[i].optionsWidget != 0){

      pluginList->addItem( plugins_[i].name );
    }

  if ( pluginList->count() > 0)
    pluginList->setCurrentRow(0);
}

void OptionsWidget::slotShowPluginOptions(const QString& _pluginName ){

  //remove old children
  for (int i = 0; i < pluginOptionsLayout->count(); ++i){
    QWidget* w = pluginOptionsLayout->itemAt(i)->widget();

    if (w != 0)
      w->setParent(0);

    pluginOptionsLayout->removeItem( pluginOptionsLayout->itemAt(i) );
  }

  //find the new optionsWidget
  for ( uint i = 0 ; i < plugins_.size(); ++i )
    if (plugins_[i].optionsWidget != 0 && plugins_[i].name == _pluginName){

      pluginOptionsLayout->addWidget( plugins_[i].optionsWidget );
      pluginOptionsLayout->addStretch();
      return;
    }
}

359
void OptionsWidget::slotApply() {
Dirk Wilden's avatar
Dirk Wilden committed
360

Jan Möbius's avatar
Jan Möbius committed
361
  //general
Dirk Wilden's avatar
Dirk Wilden committed
362 363 364
  OpenFlipper::Options::fullScreen( fullscreen->isChecked() );
  OpenFlipper::Options::splash( splash->isChecked() );
  OpenFlipper::Options::hideToolbox( !toolbox->isChecked() );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
365
  if (loggerHidden->isChecked())
Jan Möbius's avatar
Jan Möbius committed
366
    OpenFlipper::Options::loggerState( OpenFlipper::Options::Hidden );
Jan Möbius's avatar
Dennis:  
Jan Möbius committed
367 368 369 370
  else if (loggerInScene->isChecked())
    OpenFlipper::Options::loggerState( OpenFlipper::Options::InScene );
  else
    OpenFlipper::Options::loggerState( OpenFlipper::Options::Normal );
Dirk Wilden's avatar
Dirk Wilden committed
371 372 373 374
  OpenFlipper::Options::logFileEnabled( enableLogFile->isChecked() );

  //paths
  OpenFlipper::Options::logFile( logFile->text() );
Dirk Wilden's avatar
Dirk Wilden committed
375 376 377 378 379

  //viewer
  OpenFlipper::Options::wheelZoomFactor( wZoomFactor->text().toDouble() );
  OpenFlipper::Options::wheelZoomFactorShift( wZoomFactorShift->text().toDouble() );

380 381 382
  OpenFlipper::Options::restrictFrameRate( restrictFPS->isChecked() );
  OpenFlipper::Options::maxFrameRate( FPS->value() );

383 384
  OpenFlipper::Options::randomBaseColor( randomBaseColor->isChecked() );

385 386 387 388 389
  // updates
  OpenFlipper::Options::updateUrl( updateURL->text() );
  OpenFlipper::Options::updateUsername( updateUser->text() );
  OpenFlipper::Options::updatePassword( updatePass->text() );

Jan Möbius's avatar
Jan Möbius committed
390 391 392
  // Debugging
  OpenFlipper::Options::doSlotDebugging(slotDebugging->isChecked());

393 394 395
  OpenFlipper::Options::renderPicking(renderPicking->isChecked());
  OpenFlipper::Options::pickingRenderMode( pickingRenderMode->currentText() );

Dirk Wilden's avatar
Dirk Wilden committed
396 397 398 399 400 401 402
  //standardDrawMode
  std::vector< QString > mode;

  for (int i=0; i < availDrawModes->count(); i++)
    if (availDrawModes->item(i)->checkState() == Qt::Checked)
      mode.push_back( availDrawModes->item(i)->text() );

403
  OpenFlipper::Options::standardDrawMode( descriptionsToDrawMode(mode) );
Dirk Wilden's avatar
Dirk Wilden committed
404

Dirk Wilden's avatar
Dirk Wilden committed
405 406
  applyShortcuts();

407
  emit applyOptions();
Dirk Wilden's avatar
Dirk Wilden committed
408
  emit saveOptions();
Dirk Wilden's avatar
Dirk Wilden committed
409
}
410

Dirk Wilden's avatar
Dirk Wilden committed
411 412
void OptionsWidget::slotOk(){
  slotApply();
413 414 415 416 417 418
  hide();
}

void OptionsWidget::slotCancel() {
  hide();
}
Jan Möbius's avatar
Jan Möbius committed
419

Jan Möbius's avatar
Jan Möbius committed
420 421 422 423 424
void OptionsWidget::slotCheckUpdates() {

   // http://www.graphics.rwth-aachen.de/restricted/OpenFlipper-SIL/

  downloadType = VERSIONS_FILE;
425 426 427
  QString ServerMainURL = updateURL->text();
  if ( !ServerMainURL.endsWith("/") )
     ServerMainURL += "/";
Jan Möbius's avatar
Jan Möbius committed
428

429
  ServerMainURL += "Versions.ini";
Jan Möbius's avatar
Jan Möbius committed
430

431
  startDownload(ServerMainURL);
Jan Möbius's avatar
Jan Möbius committed
432 433 434
}

void OptionsWidget::slotGetUpdates() {
Jan Möbius's avatar
Jan Möbius committed
435 436 437 438 439 440 441 442
   std::cerr << "Not implemented yet: Get updates" << std::endl;
   QString url = updateURL->text();

   if ( !url.endsWith("/") )
     url += "/";


   pluginPath_.clear();
Jan Möbius's avatar
Jan Möbius committed
443
   pluginPath_ = "Plugins/";
Jan Möbius's avatar
Jan Möbius committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

   if ( OpenFlipper::Options::isWindows() ) {
      pluginPath_ += "Windows/";
   } else if ( OpenFlipper::Options::isLinux() ) {
      pluginPath_ += "Linux/";
   } else {
      std::cerr << "Unknown operating system type, aborting update" << std::endl;
      return;
   }

   if ( OpenFlipper::Options::is64bit() ) {
      pluginPath_ += "64/";
   } else if ( OpenFlipper::Options::is32bit() ) {
      pluginPath_ += "32/";
   } else {
      std::cerr << "Unknown architecture type, aborting update" << std::endl;
      return;
   }

   pluginPath_ += "Release/";

   if ( !updatedPlugins_.empty() && updatedPlugins_[0] == "Core" ) {
      std::cerr << "Core update not supported!" << std::endl;
      return;
   }

   if ( !updatedPlugins_.empty() ) {
      currentUpdateName_ = updatedPlugins_[0];
      std::cerr << "Downloading " << (url + pluginPath_ + currentUpdateName_).toStdString() << std::endl;
      updatedPlugins_.pop_front();

Jan Möbius's avatar
Jan Möbius committed
475
      downloadType = PLUGIN;
Jan Möbius's avatar
Jan Möbius committed
476 477 478 479 480 481 482 483 484 485

      startDownload(url + pluginPath_ + currentUpdateName_);
   }

}

void OptionsWidget::updateComponent() {
   std::cerr << "Todo : Update component" << std::endl;


Jan Möbius's avatar
Jan Möbius committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499

   QString sourceName = QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
                                     ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + currentUpdateName_ ;

   QString targetName = OpenFlipper::Options::applicationDirStr() + "/" + pluginPath_ + currentUpdateName_;

   QFileInfo sourceFileInfo(sourceName);
   QFileInfo targetFileInfo(targetName);

   std::cerr << sourceName.toStdString() << std::endl;
   std::cerr << targetName.toStdString() << std::endl;

   if ( ! sourceFileInfo.exists() ) {
      statusLabel->setText("Download failed!");
Jan Möbius's avatar
Jan Möbius committed
500
      slotGetUpdates();
Jan Möbius's avatar
Jan Möbius committed
501
      return;
Jan Möbius's avatar
Jan Möbius committed
502
   } else {
Jan Möbius's avatar
Jan Möbius committed
503 504
      if ( ! targetFileInfo.exists() ) {
         statusLabel->setText("plugin target does not exist");
Jan Möbius's avatar
Jan Möbius committed
505
         slotGetUpdates();
Jan Möbius's avatar
Jan Möbius committed
506 507 508 509 510
         return;
      }

      statusLabel->setText("Installing new file");

Jan Möbius's avatar
Jan Möbius committed
511
      // Remove the old plugin
Jan Möbius's avatar
Jan Möbius committed
512 513
      QFile targetFile(targetName);
      targetFile.remove();
Jan Möbius's avatar
Jan Möbius committed
514

Jan Möbius's avatar
Jan Möbius committed
515
      // copy new to old
Jan Möbius's avatar
Jan Möbius committed
516 517
      QFile::copy(sourceName,targetName);

Jan Möbius's avatar
Jan Möbius committed
518 519 520 521
      // remove downloaded file
      QFile sourceFile(sourceName);
      sourceFile.remove();

Jan Möbius's avatar
Jan Möbius committed
522
      statusLabel->setText("updated " + currentUpdateName_);
Jan Möbius's avatar
Jan Möbius committed
523
   }
Jan Möbius's avatar
Jan Möbius committed
524 525

   slotGetUpdates();
Jan Möbius's avatar
Jan Möbius committed
526 527
}

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
bool OptionsWidget::isNewer(QString _current, QString _latest) {

  QStringList latestVersionParts  = _latest.split('.');
  QStringList currentVersionParts = _current.split('.');

  bool newer = false;

  for ( int i = 0 ; i < latestVersionParts.size(); ++i ) {

    if ( i+1 > currentVersionParts.size() ) {
      // Versions are identical up to now. But latest version has additional version => updated
      newer = true;
      break;
    }

    bool ok = false;
    double latest  = latestVersionParts[i].toInt(&ok);
    double current = currentVersionParts[i].toInt(&ok);

    if ( !ok )
      std::cerr << "Error when parsing version strings!" << std::endl;

    if ( latest > current ) {
      newer = true;
      break;
    }
  }

  return newer;

}

void OptionsWidget::compareVersions() {

  QString fileName = QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
                     ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + "Versions.ini" ;

  INIFile ini;
  if ( ! ini.connect(fileName,false) ) {
    std::cerr << "Failed to connect to Versions ini file" << std::endl;
    return;
  }

  statusLabel->setText("Checking for new versions");

  bool newerVersionsAvailable = false;

575
  QString systemString = "";
576
  if ( OpenFlipper::Options::isWindows() ) {
577 578 579 580 581 582 583 584 585 586 587 588 589
    systemString = "VersionWindows";
  } else if (OpenFlipper::Options::isLinux()) {
    systemString = "VersionLinux";
  } else {
    std::cerr << "Unsupported platform for update" << std::endl;
    return;
  }

  QString coreVersion;

  if ( ini.get_entry(coreVersion, "Core" , systemString )) {
    if ( isNewer( OpenFlipper::Options::coreVersion(), coreVersion ) ) {
      newerVersionsAvailable = true;
Jan Möbius's avatar
Jan Möbius committed
590
    }
591 592 593 594 595
  }

  for ( uint i = 0 ; i < plugins_.size(); ++i ) {
    QString latestVersion;
    if ( ini.get_entry(latestVersion, plugins_[i].name , systemString )) {
Jan Möbius's avatar
Jan Möbius committed
596
      if ( isNewer(  plugins_[i].version, latestVersion ) )
597
        newerVersionsAvailable = true;
598
    }
599 600 601
  }

  if ( newerVersionsAvailable ) {
Jan Möbius's avatar
Jan Möbius committed
602
    statusLabel->setText("Updates found");
603 604
  } else {
    statusLabel->setText("No updates found");
605 606 607
  }

  ini.disconnect();
Jan Möbius's avatar
Jan Möbius committed
608 609

  updateVersionsTable();
610
}