summaryrefslogtreecommitdiff
path: root/src/fqterm/fqterm_autoupdate.cpp
blob: 0f49c38aabbe28192cbd71fedef3bcdcef618892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/***************************************************************************
 *   fqterm, a terminal emulator for both BBS and *nix.                    *
 *   Copyright (C) 2008 fqterm development group.                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.               *
 ***************************************************************************/

#include "fqterm_autoupdate.h"
#include "fqterm_config.h"
#include "fqterm_trace.h"
#include "fqterm_param.h"

#include <QDesktopServices>
#include <QFile>
#include <QHttp>
#include <QMessageBox>
#include <QString>
#include <QUrl>

namespace FQTerm {

////////////////////////////////////////////////
static int versionCompare(QString lhs, QString rhs) {
  //0 -- lhs == rhs, -1 -- lhs > rhs, 1 -- lhs < rhs
  
  int versionEnd = lhs.indexOf('-');
  if (versionEnd != -1) {
    lhs = lhs.mid(0, versionEnd);
  }
  versionEnd = rhs.indexOf('-');
  if (versionEnd != -1) {
    rhs = rhs.mid(0, versionEnd);
  }
  QStringList lhsList = lhs.split(".");
  QStringList rhsList = rhs.split(".");
  if (lhsList.size() != 3 || rhsList.size() != 3) {
    return 0; //something is wrong
  }
  for (int i = 0; i < 3; ++i) {
    if (lhsList[i].toInt() < rhsList[i].toInt()) {
      return 1;
    }
    else if (lhsList[i].toInt() > rhsList[i].toInt()) {
      return -1;
    }
  }
  return 0;
}
///////////////////////////////////////////////

void FQTermAutoUpdater::checkUpdate() {
  QString fileName = FQTermPref::getInstance()->poolDir_ + "VersionInfo";
  if (QFile::exists(fileName)) {
    QFile::remove(fileName);
  }
  versionInfoFile_ = new QFile(fileName, this);
  if (!versionInfoFile_->open(QIODevice::WriteOnly)) {
    delete versionInfoFile_;
    return;
  }
  FQ_VERIFY(connect(updateChecker_, SIGNAL(requestFinished(int, bool)),
    this, SLOT(checkRequestFinished(int, bool))));
  FQ_VERIFY(connect(updateChecker_, SIGNAL(done(bool)),
    this, SLOT(httpDone(bool))));

  const QString versionInfoURL = "http://fqterm.googlecode.com/svn/trunk/VersionInfo";
  QUrl url(versionInfoURL);
  QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
  updateChecker_->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
  if (!url.userName().isEmpty())
    updateChecker_->setUser(url.userName(), url.password());
  checkRequestId_ =  updateChecker_->get(url.path(), versionInfoFile_);
}

QString FQTermAutoUpdater::getNewestVersion() {
#if defined(WIN32)
  const QString platform = "FQWin";
#elif defined(__unix__) && !defined(__APPLE__)
  const QString platform = "FQLinux";
#else
  const QString platform = "FQMac";
#endif
  FQTermConfig versionInfo(FQTermPref::getInstance()->poolDir_ + "VersionInfo");
  QString ver = versionInfo.getItemValue(platform, "version");
  if (ver.isEmpty()) {
    return FQTERM_VERSION_STRING;
  }
  return ver;
}

void FQTermAutoUpdater::promptUpdate() {
  const QString updateURL = "http://code.google.com/p/fqterm/downloads/list";
  QMessageBox msgBox(QMessageBox::Information, tr("FQTerm Update Notifier"), 
    tr("FQTerm update available.\nPress OK to visit our download list,\nDiscard for a future prompt,\nIgnore for no more notification."),
    QMessageBox::Ok | QMessageBox::Discard | QMessageBox::Ignore);
  switch (msgBox.exec()) {
   case QMessageBox::Ok:
     QDesktopServices::openUrl(updateURL);
     break;
   case QMessageBox::Discard:
     break;
   case QMessageBox::Ignore:
     config_->setItemValue("global", "newestversion", getNewestVersion());
     config_->setItemValue("global", "updateprompt", "0");
     break;
  } 
}

void FQTermAutoUpdater::checkRequestFinished(int requestId, bool error){
  
  if (checkDone_ || requestId != checkRequestId_) {
    return;
  }
  if (!error) {
    checkDone_ = true;
  }
}

void FQTermAutoUpdater::httpDone(bool err)  {
  if (!err) {
    versionInfoFile_->flush();
    if (checkDone_) {
      QString newestVersionRecorded = config_->getItemValue("global", "newestversion");
      if (newestVersionRecorded.isEmpty()) {
        newestVersionRecorded = FQTERM_VERSION_STRING;
        config_->setItemValue("global", "newestversion", newestVersionRecorded);
      }
      QString newestVersionReleased = getNewestVersion();

      QString updatePrompt = config_->getItemValue("global", "updateprompt");
      if (updatePrompt == "0") {
        //if newest version > recorded version, still prompt.
        if (versionCompare(newestVersionRecorded, newestVersionReleased) > 0) {
          config_->setItemValue("global", "updateprompt", "1");
          promptUpdate();
        }
      }
      else {
        config_->setItemValue("global", "updateprompt", "1");
        //if newest version > version in use, prompt.
        if (versionCompare(FQTERM_VERSION_STRING, newestVersionReleased) > 0) {
          promptUpdate();
        }
      }
    }
  }
  deleteLater();
}

FQTermAutoUpdater::FQTermAutoUpdater(QObject* parent, FQTermConfig* config) 
: QObject(parent),
  config_(config),
  versionInfoFile_(0),
  checkDone_(false) {
  updateChecker_ = new QHttp(this);
}

FQTermAutoUpdater::~FQTermAutoUpdater()
{

}

} //FQTerm namespace

#include "fqterm_autoupdate.moc"