~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to gui/post_install_dialog/post_install_dialog.cc

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2010, Google Inc.
 
2
// All rights reserved.
 
3
//
 
4
// Redistribution and use in source and binary forms, with or without
 
5
// modification, are permitted provided that the following conditions are
 
6
// met:
 
7
//
 
8
//     * Redistributions of source code must retain the above copyright
 
9
// notice, this list of conditions and the following disclaimer.
 
10
//     * Redistributions in binary form must reproduce the above
 
11
// copyright notice, this list of conditions and the following disclaimer
 
12
// in the documentation and/or other materials provided with the
 
13
// distribution.
 
14
//     * Neither the name of Google Inc. nor the names of its
 
15
// contributors may be used to endorse or promote products derived from
 
16
// this software without specific prior written permission.
 
17
//
 
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
29
 
 
30
#include "gui/post_install_dialog/post_install_dialog.h"
 
31
 
 
32
#ifdef OS_WINDOWS
 
33
#include <windows.h>
 
34
#endif
 
35
 
 
36
#include <QtGui/QtGui>
 
37
#include "base/base.h"
 
38
#include "base/process.h"
 
39
#include "base/run_level.h"
 
40
#include "base/util.h"
 
41
#include "base/win_util.h"
 
42
#include "dictionary/user_dictionary_importer.h"
 
43
#include "dictionary/user_dictionary_storage.h"
 
44
#include "dictionary/user_dictionary_util.h"
 
45
#include "usage_stats/usage_stats.h"
 
46
 
 
47
 
 
48
#ifdef OS_WINDOWS
 
49
#include "win32/base/imm_util.h"
 
50
#endif
 
51
 
 
52
namespace mozc {
 
53
namespace gui {
 
54
 
 
55
PostInstallDialog::PostInstallDialog()
 
56
    : logoff_required_(false),
 
57
      storage_(
 
58
          new UserDictionaryStorage(
 
59
              UserDictionaryUtil::GetUserDictionaryFileName())) {
 
60
#ifdef OS_WINDOWS
 
61
  if (!mozc::ImeUtil::IsCuasEnabled()) {
 
62
    logoff_required_ = true;
 
63
    mozc::ImeUtil::SetCuasEnabled(true);
 
64
  }
 
65
#endif
 
66
 
 
67
  setupUi(this);
 
68
  setWindowFlags(Qt::WindowSystemMenuHint |
 
69
                 Qt::MSWindowsFixedSizeDialogHint |
 
70
                 Qt::WindowStaysOnTopHint);
 
71
  setWindowModality(Qt::NonModal);
 
72
 
 
73
  QObject::connect(logoffNowButton,
 
74
                   SIGNAL(clicked()),
 
75
                   this,
 
76
                   SLOT(OnLogoffNow()));
 
77
  QObject::connect(logoffLaterButton,
 
78
                   SIGNAL(clicked()),
 
79
                   this,
 
80
                   SLOT(OnLogoffLater()));
 
81
  QObject::connect(okButton,
 
82
                   SIGNAL(clicked()),
 
83
                   this,
 
84
                   SLOT(OnOk()));
 
85
 
 
86
  // We change buttons to be displayed depending on the condition this dialog
 
87
  // is launched.
 
88
  // The following table summarizes which buttons are displayed by conditions.
 
89
  //
 
90
  // ----------------------------------------------
 
91
  // |   OK   | Later  |  Now   |  help  | logoff |
 
92
  // ----------------------------------------------
 
93
  // |   D    |   N    |   N    |  true  |  true  |
 
94
  // |   D    |   N    |   N    |  true  |  false |
 
95
  // |   N    |   D    |   D    |  false |  true  |
 
96
  // |   D    |   N    |   N    |  false |  false |
 
97
  // ----------------------------------------------
 
98
  //
 
99
  // The followings are meanings of the words used in the table.
 
100
  // OK     : okButton
 
101
  // Later  : logoffLaterButton
 
102
  // Now    : logoffNowButton
 
103
  // help   : The result of IsShowHelpPageRequired()
 
104
  // logoff : The result of IsLogoffRequired()
 
105
  // N      : not displayed
 
106
  // D      : displayed
 
107
  //
 
108
  if (IsShowHelpPageRequired()) {
 
109
    usage_stats::UsageStats::IncrementCount("PostInstallShowPageRequired");
 
110
    thanksLabel->setText(tr("Thanks for installing.\n"
 
111
                            "You need to configure your computer before using "
 
112
                            "Mozc. Please follow the "
 
113
                            "instructions on the help page."));
 
114
    logoffNowButton->setVisible(false);
 
115
    logoffLaterButton->setVisible(false);
 
116
  } else {
 
117
    if (logoff_required()) {
 
118
      usage_stats::UsageStats::IncrementCount("PostInstallLogoffRequired");
 
119
      thanksLabel->setText(tr("Thanks for installing.\nYou must log off before "
 
120
                              "using Mozc."));
 
121
      // remove OK button and move the other buttons to right.
 
122
      const int rows = gridLayout->rowCount();
 
123
      const int cols = gridLayout->columnCount();
 
124
      okButton->setVisible(false);
 
125
      gridLayout->removeWidget(okButton);
 
126
      gridLayout->addWidget(logoffNowButton, rows - 1, cols - 2);
 
127
      gridLayout->addWidget(logoffLaterButton, rows - 1, cols - 1);
 
128
    } else {
 
129
      usage_stats::UsageStats::IncrementCount("PostInstallNothingRequired");
 
130
      logoffNowButton->setVisible(false);
 
131
      logoffLaterButton->setVisible(false);
 
132
      gridLayout->removeWidget(logoffNowButton);
 
133
      gridLayout->removeWidget(logoffLaterButton);
 
134
    }
 
135
  }
 
136
 
 
137
  // set the default state of migrateDefaultIMEUserDictionaryCheckBox
 
138
  const bool status = (!RunLevel::IsElevatedByUAC() && storage_->Lock());
 
139
  migrateDefaultIMEUserDictionaryCheckBox->setVisible(status);
 
140
 
 
141
  // import MS-IME by default
 
142
  migrateDefaultIMEUserDictionaryCheckBox->setChecked(true);
 
143
}
 
144
 
 
145
PostInstallDialog::~PostInstallDialog() {
 
146
}
 
147
 
 
148
bool PostInstallDialog::logoff_required() {
 
149
  return logoff_required_;
 
150
}
 
151
 
 
152
bool PostInstallDialog::ShowHelpPageIfRequired() {
 
153
  if (PostInstallDialog::IsShowHelpPageRequired()) {
 
154
    const char kHelpPageUrl[] =
 
155
        "http://www.google.com/support/ime/japanese/bin/answer.py?hl=jp&answer="
 
156
        "166771";
 
157
    return mozc::Process::OpenBrowser(kHelpPageUrl);
 
158
  }
 
159
  return false;
 
160
}
 
161
 
 
162
// NOTE(mazda): UsageStats class is not currently multi-process safe so it is
 
163
// possible that usagestats is incorrectly collected.
 
164
// For example if the user activate Mozc in another application before closing
 
165
// this dialog, the usagestats collected in the application can be overwritten
 
166
// when this dialog is closed.
 
167
// But this case is very rare since this dialog is launched immediately after
 
168
// installation.
 
169
// So we accept the potential error until this limitation is fixed.
 
170
void PostInstallDialog::OnLogoffNow() {
 
171
  usage_stats::UsageStats::IncrementCount("PostInstallLogoffNowButton");
 
172
  ApplySettings();
 
173
#ifdef OS_WINDOWS
 
174
  mozc::WinUtil::Logoff();
 
175
#else
 
176
  // not supported on Mac and Linux
 
177
#endif  // OS_WINDOWS
 
178
  done(QDialog::Accepted);
 
179
}
 
180
 
 
181
void PostInstallDialog::OnLogoffLater() {
 
182
  usage_stats::UsageStats::IncrementCount("PostInstallLogoffLaterButton");
 
183
  ApplySettings();
 
184
  done(QDialog::Rejected);
 
185
}
 
186
 
 
187
void PostInstallDialog::OnOk() {
 
188
  usage_stats::UsageStats::IncrementCount("PostInstallOkButton");
 
189
  ApplySettings();
 
190
  done(QDialog::Accepted);
 
191
}
 
192
 
 
193
void PostInstallDialog::reject() {
 
194
  usage_stats::UsageStats::IncrementCount("PostInstallRejectButton");
 
195
  done(QDialog::Rejected);
 
196
}
 
197
 
 
198
void PostInstallDialog::ApplySettings() {
 
199
#ifdef OS_WINDOWS
 
200
  if (setAsDefaultCheckBox->isChecked()) {
 
201
    usage_stats::UsageStats::IncrementCount("PostInstallSetDefault");
 
202
    ImeUtil::SetDefault();
 
203
  } else {
 
204
    usage_stats::UsageStats::IncrementCount("PostInstallNotSetDefault");
 
205
  }
 
206
 
 
207
  if (migrateDefaultIMEUserDictionaryCheckBox->isChecked() &&
 
208
      migrateDefaultIMEUserDictionaryCheckBox->isVisible()) {
 
209
    storage_->Load();
 
210
    // create UserDictionary if the current user dictionary is empty
 
211
    if (!storage_->Exists()) {
 
212
      const QString name = tr("User Dictionary 1");
 
213
      uint64 dic_id = 0;
 
214
      if (!storage_->CreateDictionary(name.toStdString(),
 
215
                                      &dic_id)) {
 
216
        LOG(ERROR) << "Failed to create a new dictionary.";
 
217
        return;
 
218
      }
 
219
    }
 
220
 
 
221
    // Import MS-IME's dictionary to an unique dictionary labeled
 
222
    // as "MS-IME"
 
223
    uint64 dic_id = 0;
 
224
    const QString name = tr("MS-IME User Dictionary");
 
225
    for (size_t i = 0; i < storage_->dictionaries_size(); ++i) {
 
226
      if (storage_->dictionaries(i).name() == name.toStdString()) {
 
227
        dic_id = storage_->dictionaries(i).id();
 
228
        break;
 
229
      }
 
230
    }
 
231
 
 
232
    if (dic_id == 0) {
 
233
      if (!storage_->CreateDictionary(name.toStdString(),
 
234
                                      &dic_id)) {
 
235
        LOG(ERROR) << "Failed to create a new dictionary.";
 
236
        return;
 
237
      }
 
238
    }
 
239
 
 
240
    UserDictionaryStorage::UserDictionary *dic =
 
241
        storage_->GetUserDictionary(dic_id);
 
242
    if (dic == NULL) {
 
243
      LOG(ERROR) << "GetUserDictionary returned NULL";
 
244
      return;
 
245
    }
 
246
 
 
247
    if (UserDictionaryImporter::ImportFromMSIME(dic) !=
 
248
        UserDictionaryImporter::IMPORT_NO_ERROR) {
 
249
      LOG(ERROR) << "ImportFromMSIME failed";
 
250
    }
 
251
 
 
252
    storage_->Save();
 
253
  }
 
254
#else
 
255
  // not supported on Mac and Linux
 
256
#endif  // OS_WINDOWS
 
257
}
 
258
 
 
259
bool PostInstallDialog::IsShowHelpPageRequired() {
 
260
#ifdef OS_WINDOWS
 
261
  return !ImeUtil::IsCtfmonRunning();
 
262
#else
 
263
  // not supported on Mac and Linux
 
264
  return false;
 
265
#endif  // OS_WINDOWS
 
266
}
 
267
 
 
268
}  // namespace gui
 
269
}  // namespace mozc