optionsWidget.cc 19.4 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 26 27 28 29 30 31 32 33 34 35 36 37
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision: 1909 $
//   $Author: wilden $
//   $Date: 2008-06-03 18:45:21 +0200 (Tue, 03 Jun 2008) $
//
//=============================================================================




#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, QWidget *parent)
Jan Möbius's avatar
Jan Möbius committed
45 46 47 48
  : QWidget(parent),
    plugins_(_plugins),
    coreKeys_(_core)

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

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

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

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

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

      availDrawModes->addItem( item );
    }
74

Dirk Wilden's avatar
Dirk Wilden committed
75 76
    mode = mode<<1;

77
  }
Jan Möbius's avatar
Jan Möbius committed
78

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

Jan Möbius's avatar
Jan Möbius committed
81 82 83 84 85 86 87
  // 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
88

Jan Möbius's avatar
Jan Möbius committed
89 90 91

  progressDialog = new QProgressDialog(this);
  connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
92 93 94 95 96 97 98 99


  //colordialog
  connect(backgroundButton, SIGNAL(clicked()), this, SLOT(getBackgroundColor()) );
}

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

101 102 103 104 105
  OpenFlipper::Options::defaultBackgroundColor( newColor.rgb() );

  QPixmap color(16,16);
  color.fill(QColor(OpenFlipper::Options::defaultBackgroundColor()));
  backgroundButton->setIcon( QIcon(color) );
106 107
}

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

Jan Möbius's avatar
Jan Möbius committed
110
  //general
Dirk Wilden's avatar
Dirk Wilden committed
111 112 113 114
  fullscreen->setChecked( OpenFlipper::Options::fullScreen() );
  splash->setChecked( OpenFlipper::Options::splash() );
  toolbox->setChecked( !OpenFlipper::Options::hideToolbox() );
  logger->setChecked( !OpenFlipper::Options::hideLogger() );
Dirk Wilden's avatar
Dirk Wilden committed
115
  enableLogFile->setChecked( OpenFlipper::Options::logFileEnabled() );
Jan Möbius's avatar
Jan Möbius committed
116

Dirk Wilden's avatar
Dirk Wilden committed
117 118
  //paths
  logFile->setText( OpenFlipper::Options::logFile() );
Dirk Wilden's avatar
Dirk Wilden committed
119 120 121 122 123 124

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

125 126 127
  restrictFPS->setChecked( OpenFlipper::Options::restrictFrameRate() );
  FPS->setValue( OpenFlipper::Options::maxFrameRate() );

128 129 130 131
  QPixmap color(16,16);
  color.fill(QColor(OpenFlipper::Options::defaultBackgroundColor()));
  backgroundButton->setIcon( QIcon(color) );

132 133 134 135 136
  // 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
137 138 139
  // debugging
  slotDebugging->setChecked(OpenFlipper::Options::doSlotDebugging());

140 141 142
  //set drawmodes


Dirk Wilden's avatar
Dirk Wilden committed
143
  //keyBindings
144 145
  keyTree->clear();

Dirk Wilden's avatar
Dirk Wilden committed
146 147 148 149 150 151 152 153 154 155 156 157 158
  keyTree->setColumnCount ( 2 );

  QStringList headerdata;
  headerdata << "Action" << "Shortcut";
  keyTree->setHeaderLabels(headerdata);

  //add Core Keys
  QTreeWidgetItem * core = new QTreeWidgetItem(keyTree, QStringList("CoreWidget"));

  QList<QTreeWidgetItem *> keys;

  for (uint i=0; i < coreKeys_.size(); i++){
    QStringList row;
Jan Möbius's avatar
Jan Möbius committed
159 160
    QKeySequence keySequence( coreKeys_[i].key + coreKeys_[i].modifiers );
    row << coreKeys_[i].description << keySequence.toString();
Dirk Wilden's avatar
Dirk Wilden committed
161 162 163 164 165 166 167 168 169 170
    keys.append(new QTreeWidgetItem(core, row));
  }

  core->addChildren(keys);
  keyTree->addTopLevelItem( core );



  QList<QTreeWidgetItem *> plugins;

Dirk Wilden's avatar
Dirk Wilden committed
171 172
  int off = 0;

Dirk Wilden's avatar
Dirk Wilden committed
173
  for (uint i=0; i < plugins_.size(); i++){
Dirk Wilden's avatar
Dirk Wilden committed
174 175
//      if (( (plugins_[i]).keys).size() == 0)
//        continue;
176

Dirk Wilden's avatar
Dirk Wilden committed
177 178 179 180 181 182
    plugins.append(new QTreeWidgetItem(keyTree, QStringList( plugins_[i].name )));

    QList<QTreeWidgetItem *> keys;

    for (int k=0; k < plugins_[i].keys.count(); k++){
      QStringList row;
Jan Möbius's avatar
Jan Möbius committed
183 184
      QKeySequence keySequence( plugins_[i].keys[k].key + plugins_[i].keys[k].modifiers );
      row << plugins_[i].keys[k].description << keySequence.toString();
Dirk Wilden's avatar
Dirk Wilden committed
185
      keys.append(new QTreeWidgetItem(plugins[i-off], row));
Dirk Wilden's avatar
Dirk Wilden committed
186 187
    }

Dirk Wilden's avatar
Dirk Wilden committed
188 189 190 191 192 193 194
    if (keys.count() > 0)
      plugins[i-off]->addChildren(keys);
    else{
      delete plugins[i-off];
      plugins.removeLast();
      off++;
    }
Dirk Wilden's avatar
Dirk Wilden committed
195 196
  }

197 198
  if (plugins.count() > 0)
    keyTree->addTopLevelItems( plugins );
Dirk Wilden's avatar
Dirk Wilden committed
199

200
  keyTree->setColumnWidth(0,350);
201 202 203 204 205 206


  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
207
  std::vector< QString > dm = drawModeToDescriptions( OpenFlipper::Options::standardDrawMode() );
208 209 210 211 212 213 214 215

  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
216 217 218 219 220 221 222

  updateVersionsTable();

}

void OptionsWidget::updateVersionsTable() {

Jan Möbius's avatar
Jan Möbius committed
223 224
  updatedPlugins_.clear();

Jan Möbius's avatar
Jan Möbius committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
  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
261
    if ( isNewer(OpenFlipper::Options::coreVersion(),coreVersion) ) {
Jan Möbius's avatar
Jan Möbius committed
262
      currentBrush.setColor(Qt::red);
Jan Möbius's avatar
Jan Möbius committed
263 264
      updatedPlugins_ << "Core";
    } else if ( isNewer(coreVersion , OpenFlipper::Options::coreVersion()) )
265
      currentBrush.setColor(Qt::blue);
Jan Möbius's avatar
Jan Möbius committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    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
298
      if ( isNewer(plugins_[i].version,latestVersion) ) {
299
         currentBrush.setColor(Qt::red);
Jan Möbius's avatar
Jan Möbius committed
300 301 302
         QFileInfo pluginFile(plugins_[i].path );
         updatedPlugins_ << pluginFile.fileName();
      } else if ( isNewer(latestVersion,plugins_[i].version) )
303
         currentBrush.setColor(Qt::blue);
Jan Möbius's avatar
Jan Möbius committed
304
      else
305
         currentBrush.setColor(Qt::green);
Jan Möbius's avatar
Jan Möbius committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

      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
331 332
  updateButton->setEnabled(!updatedPlugins_.empty());

333 334 335
}

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

Jan Möbius's avatar
Jan Möbius committed
337
  //general
Dirk Wilden's avatar
Dirk Wilden committed
338 339 340 341
  OpenFlipper::Options::fullScreen( fullscreen->isChecked() );
  OpenFlipper::Options::splash( splash->isChecked() );
  OpenFlipper::Options::hideToolbox( !toolbox->isChecked() );
  OpenFlipper::Options::hideLogger( !logger->isChecked() );
Dirk Wilden's avatar
Dirk Wilden committed
342 343 344 345
  OpenFlipper::Options::logFileEnabled( enableLogFile->isChecked() );

  //paths
  OpenFlipper::Options::logFile( logFile->text() );
Dirk Wilden's avatar
Dirk Wilden committed
346 347 348 349 350 351

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

352 353 354
  OpenFlipper::Options::restrictFrameRate( restrictFPS->isChecked() );
  OpenFlipper::Options::maxFrameRate( FPS->value() );

355 356 357 358 359
  // 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
360 361 362
  // Debugging
  OpenFlipper::Options::doSlotDebugging(slotDebugging->isChecked());

Dirk Wilden's avatar
Dirk Wilden committed
363 364 365 366 367 368 369
  //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() );

370
  OpenFlipper::Options::standardDrawMode( descriptionsToDrawMode(mode) );
Dirk Wilden's avatar
Dirk Wilden committed
371

372
  emit applyOptions();
Dirk Wilden's avatar
Dirk Wilden committed
373
  emit saveOptions();
374

375 376 377 378 379 380
  hide();
}

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

Jan Möbius's avatar
Jan Möbius committed
382 383
void OptionsWidget::startDownload( QString _url ) {
   QUrl url(_url);
Jan Möbius's avatar
Jan Möbius committed
384

Jan Möbius's avatar
Jan Möbius committed
385 386
   // If username or passowrd are supplied, use them
   if ( ! updateUser->text().isEmpty() )
Jan Möbius's avatar
Jan Möbius committed
387 388 389 390 391
    url.setUserName(updateUser->text());

  if ( ! updatePass->text().isEmpty() )
    url.setPassword(updatePass->text());

Jan Möbius's avatar
Jan Möbius committed
392 393 394 395

  QFileInfo urlInfo(_url);

  // Download the file to the Home Directory
Jan Möbius's avatar
Jan Möbius committed
396
  QFileInfo fileInfo( QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
Jan Möbius's avatar
Jan Möbius committed
397 398
                      ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + urlInfo.fileName() );

Jan Möbius's avatar
Jan Möbius committed
399 400 401 402 403 404 405 406
  QString fileName = fileInfo.filePath();

  if (QFile::exists(fileName)) {
    QFile::remove(fileName);
  }

  file = new QFile(fileName);
  if (!file->open(QIODevice::WriteOnly)) {
Jan Möbius's avatar
Jan Möbius committed
407
    std::cerr << "Unable to Open local file " + fileName.toStdString() + " for writing" << std::endl;
Jan Möbius's avatar
Jan Möbius committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    delete file;
    file = 0;
  } else {
    QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
    http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());

    if (!url.userName().isEmpty())
        http->setUser(url.userName(), url.password());

    httpRequestAborted = false;
    QByteArray path = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/");
    if (path.isEmpty())
      path = "/";

    httpGetId = http->get(path, file);

Jan Möbius's avatar
Jan Möbius committed
424
    statusLabel->setText("Getting Versions file from Server");
Jan Möbius's avatar
Jan Möbius committed
425 426 427 428 429

    progressDialog->setWindowTitle(tr("HTTP"));
    progressDialog->setLabelText(tr("Downloading %1.").arg(fileName));
    checkUpdateButton->setEnabled(false);
  }
Jan Möbius's avatar
Jan Möbius committed
430 431 432 433 434 435 436 437

}

void OptionsWidget::slotCheckUpdates() {

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

  downloadType = VERSIONS_FILE;
438 439 440
  QString ServerMainURL = updateURL->text();
  if ( !ServerMainURL.endsWith("/") )
     ServerMainURL += "/";
Jan Möbius's avatar
Jan Möbius committed
441

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

444
  startDownload(ServerMainURL);
Jan Möbius's avatar
Jan Möbius committed
445 446 447
}

void OptionsWidget::slotGetUpdates() {
Jan Möbius's avatar
Jan Möbius committed
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 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
   std::cerr << "Not implemented yet: Get updates" << std::endl;
   QString url = updateURL->text();

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


   pluginPath_.clear();
   pluginPath_ = "Plugins";

   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();

      downloadType = COMPONENT;

      startDownload(url + pluginPath_ + currentUpdateName_);
   }

}

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

   QFileInfo updateFileInfo (QDir::home().absolutePath() + OpenFlipper::Options::dirSeparator() +
                             ".OpenFlipper" + OpenFlipper::Options::dirSeparator() + currentUpdateName_);

   if ( ! updateFileInfo.exists() ) {
      std::cerr << "Download failed?! " << std::endl;
   } else {

   }
//    QString pluginPath_;
Jan Möbius's avatar
Jan Möbius committed
507 508
}

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 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
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;

556
  QString systemString = "";
557
  if ( OpenFlipper::Options::isWindows() ) {
558 559 560 561 562 563 564 565 566 567 568 569 570
    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
571
    }
572 573 574 575 576
  }

  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
577
      if ( isNewer(  plugins_[i].version, latestVersion ) )
578
        newerVersionsAvailable = true;
579
    }
580 581 582
  }

  if ( newerVersionsAvailable ) {
Jan Möbius's avatar
Jan Möbius committed
583
    statusLabel->setText("Updates found");
584 585
  } else {
    statusLabel->setText("No updates found");
586 587 588
  }

  ini.disconnect();
Jan Möbius's avatar
Jan Möbius committed
589 590

  updateVersionsTable();
591 592
}

Jan Möbius's avatar
Jan Möbius committed
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
void OptionsWidget::httpRequestFinished(int requestId, bool error)
{
    if (requestId != httpGetId)
        return;
    if (httpRequestAborted) {
        if (file) {
            file->close();
            file->remove();
            delete file;
            file = 0;
        }

        progressDialog->hide();
        return;
    }

    if (requestId != httpGetId)
        return;

    progressDialog->hide();
    file->close();

    if (error) {
        file->remove();
Jan Möbius's avatar
Jan Möbius committed
617
        statusLabel->setText(tr("Download failed: %1.").arg(http->errorString()));
Jan Möbius's avatar
Jan Möbius committed
618 619 620 621 622
        QMessageBox::information(this, tr("HTTP"),
                                  tr("Download failed: %1.")
                                  .arg(http->errorString()));
    } else {
        QString fileName = QFileInfo(QUrl(updateURL->text()).path()).fileName();
Jan Möbius's avatar
Jan Möbius committed
623
        statusLabel->setText(tr("Downloaded %1").arg(file->fileName() ));
Jan Möbius's avatar
Jan Möbius committed
624 625 626 627 628
    }

    checkUpdateButton->setEnabled(true);
    delete file;
    file = 0;
629 630 631

    if ( !error ) {
      if ( downloadType == VERSIONS_FILE )
Jan Möbius's avatar
Jan Möbius committed
632 633 634
         compareVersions();
      if ( downloadType == COMPONENT )
         updateComponent();
635
    }
Jan Möbius's avatar
Jan Möbius committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
}

void OptionsWidget::readResponseHeader(const QHttpResponseHeader &responseHeader)
{
  switch (responseHeader.statusCode()) {
  case 200:                   // Ok
  case 301:                   // Moved Permanently
  case 302:                   // Found
  case 303:                   // See Other
  case 307:                   // Temporary Redirect
    // these are not error conditions
    break;

  default:
    QMessageBox::information(this, tr("HTTP"),
                              tr("Download failed: %1.")
                              .arg(responseHeader.reasonPhrase()));
    statusLabel->setText("Download failed: " + responseHeader.reasonPhrase());
    httpRequestAborted = true;
    progressDialog->hide();
    http->abort();
  }
 }

void OptionsWidget::cancelDownload()
{
  statusLabel->setText(tr("download canceled."));
  httpRequestAborted = true;
  http->abort();
  checkUpdateButton->setEnabled(true);
}

void OptionsWidget::updateDataReadProgress(int bytesRead, int totalBytes)
{
  if (httpRequestAborted)
    return;

  progressDialog->setMaximum(totalBytes);
  progressDialog->setValue(bytesRead);
}



Jan Möbius's avatar
Jan Möbius committed
679 680