-
Notifications
You must be signed in to change notification settings - Fork 14
/
timerdialog.cpp
445 lines (384 loc) · 17.3 KB
/
timerdialog.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
244
245
246
247
248
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
281
282
283
284
285
286
287
288
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
314
315
316
317
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
* Copyright 2013-2014 KanMemo Project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "timerdialog.h"
#include "ui_timerdialog.h"
#include "kanmusumemory_global.h"
#include "twitterinfo.h"
#include <QQmlContext>
#include <QQuickItem>
#include <QtQml/QQmlEngine>
#include <QDateTime>
#include <QtCore/QDebug>
static const int DIALOG_MARGIN = 30;
TimerDialog::TimerDialog(QWidget *parent
, QSystemTrayIcon *trayIcon
, QSettings *settings) :
QDialog(parent),
m_viewer(NULL),
m_trayIcon(trayIcon),
m_settings(settings),
m_oauth(this),
m_status(this),
ui(new Ui::TimerDialog)
{
ui->setupUi(this);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
connect(&m_timerdata, &TimerData::tweetFinishedChanged, [this](){
if(m_settings != NULL){
m_settings->beginGroup(QStringLiteral(SETTING_TIMER));
m_settings->setValue(QStringLiteral(SETTING_TIMER_TWEETFINISHED), m_timerdata.tweetFinished());
m_settings->endGroup();
}
});
connect(&m_status, &Status::loadingChanged, [this](bool loading) {
if(loading){
//送信中
}else{
//終わった
tweetTimerMessage(QStringList());
}
});
loadSettings();
//QMLの読み込み
m_viewer = new QtQuick2ApplicationViewer(windowHandle());
connect(m_viewer->engine(), SIGNAL(quit()), this, SLOT(closeQml()));
//C++のデータをQML側へ公開
m_viewer->rootContext()->setContextProperty("timerData", &m_timerdata);
//QML設定して表示
m_viewer->setResizeMode(QQuickView::SizeViewToRootObject);
m_viewer->setSource(QUrl("qrc:///qml/KanmusuMemory/timerDialog.qml"));
ui->layout->addWidget(QWidget::createWindowContainer(m_viewer, this));
//サイズ調節
// QSize contentSize = m_viewer->rootObject()->childrenRect().toRect().size() + QSize(DIALOG_MARGIN,DIALOG_MARGIN);
// setMinimumSize(contentSize);
// setMaximumSize(contentSize);
QSize size = QSize(m_viewer->rootObject()->width(), m_viewer->rootObject()->height());
// setMinimumSize(size);
setMaximumSize(size);
connect(m_viewer->rootObject(), &QQuickItem::widthChanged, [this](){
resize(m_viewer->rootObject()->width(), height());
});
connect(m_viewer->rootObject(), &QQuickItem::heightChanged, [this](){
resize(m_viewer->rootObject()->width(), m_viewer->rootObject()->height());
});
m_timer.start(10000);
}
TimerDialog::~TimerDialog()
{
//設定保存
saveSettings();
if(m_viewer != NULL)
delete m_viewer;
delete ui;
}
void TimerDialog::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
if(m_viewer != NULL){
m_viewer->setGeometry(QRect(0, 0, width(), height()));
}
}
void TimerDialog::showEvent(QShowEvent *event)
{
Q_UNUSED(event);
}
void TimerDialog::closeQml()
{
// accept();
}
//全体のカウント処理
void TimerDialog::timeout()
{
QString message;
QStringList messages;
QStringList numbername;
numbername << tr("1st") << tr("2nd") << tr("3rd") << tr("4th") << tr("5th") << tr("6th") << tr("7th") << tr("8th");
//入渠
message = "";
for(int i=0; i<m_timerdata.dockingRunning().length(); i++){
if(m_timerdata.dockingRunning()[i]){
// qDebug() << i << ":set=" << m_timerdata.dockingTime()[i]
// << ",start=" << m_timerdata.dockingStart()[i];
if(checkKanmemoTimerTimeout(m_timerdata.dockingTime()[i]
, m_timerdata.dockingStart()[i])){
// qDebug() << "doc:" << i << ":" << "timeout";
m_timerdata.setRunning(0, i, false);
//メッセージ追加
if(message.length() == 0){
message = tr("It is time that Kanmusu is up from the ");
}else{
message.append(",");
}
message.append(numbername[i]);
// messages.append(tr("It is time that Kanmusu is up from the %1 bath.").arg(numbername[i]));
// messages.append(tr("ドック%1の艦娘がお風呂から上がる頃です。").arg(i+1));
}
}
}
if(message.length() > 0){
message.append(tr(" bath."));
messages.append(message);
}
//遠征
message = "";
for(int i=0; i<m_timerdata.expeditionRunning().length(); i++){
if(m_timerdata.expeditionRunning()[i]){
if(checkKanmemoTimerTimeout(m_timerdata.expeditionTime()[i]
, m_timerdata.expeditionStart()[i])){
// qDebug() << "exp:" << i << ":" << "timeout";
m_timerdata.setRunning(1, i, false);
//メッセージ追加
if(message.length() == 0){
message = tr("It is time the ");
}else{
message.append(",");
}
message.append(numbername[i+1]);
// messages.append(tr("It is time the %1 Fleet is returned.").arg(numbername[i+1]));
}
}
}
if(message.length() > 0){
message.append(tr(" Fleet is returned."));
messages.append(message);
}
//建造
message = "";
for(int i=0; i<m_timerdata.constructionRunning().length(); i++){
if(m_timerdata.constructionRunning()[i]){
if(checkKanmemoTimerTimeout(m_timerdata.constructionTime()[i]
, m_timerdata.constructionStart()[i])){
// qDebug() << "con:" << i << ":" << "timeout";
m_timerdata.setRunning(2, i, false);
//メッセージ追加
if(message.length() == 0){
message = tr("It is time the Kanmusu of ");
}else{
message.append(",");
}
message.append(numbername[i]);
// messages.append(tr("It is time the Kanmusu of %1 dock is completed.").arg(numbername[i]));
}
}
}
if(message.length() > 0){
message.append(tr(" dock is completed."));
messages.append(message);
}
if(messages.length() > 0){
//システムトレイ
showTimerMessage(messages);
//つぶやく
tweetTimerMessage(messages);
}
}
QString TimerDialog::lastTimerSelectGuideUpdateDate() const
{
return m_timerdata.lastUpdateDate();
}
void TimerDialog::setLastTimerSelectGuideUpdateDate(const QString &lastTimerSelectGuideUpdateDate)
{
m_timerdata.setLastUpdateDate(lastTimerSelectGuideUpdateDate);
}
//タイマーの設定時間を更新する
void TimerDialog::updateTimerSetting(const int kind, const int fleet_no, const qint64 remain, const qint64 total)
{
qint64 now = QDateTime::currentMSecsSinceEpoch();
int index = fleet_no - 2;
if(index < 0) index = 0;
if(index >= 3) index = 2;
if(m_viewer != NULL){
QMetaObject::invokeMethod(m_viewer->rootObject(), "stop"
, Q_ARG(QVariant, QVariant::fromValue(kind))
, Q_ARG(QVariant, QVariant::fromValue(index)));
}
m_timerdata.setTime(kind, index, total);
m_timerdata.setStartTime(kind, index, now - (total - remain));
m_timerdata.setRunning(kind, index, true);
}
const bool TimerDialog::tweetFinished() const
{
return m_timerdata.tweetFinished();
}
void TimerDialog::setTweetFinished(bool tweet)
{
m_timerdata.setTweetFinished(tweet);
}
void TimerDialog::setAlarmMute(bool mute)
{
m_timerdata.setAlarmMute(mute);
}
//時間が来ているかチェックする
bool TimerDialog::checkKanmemoTimerTimeout(qint64 settime, qint64 starttime)
{
qint64 now = QDateTime::currentMSecsSinceEpoch();
if(now >= (settime + starttime)){
//時間が来た
return true;
}else{
return false;
}
}
void TimerDialog::showTimerMessage(const QStringList &messages)
{
if(m_trayIcon == NULL)
return;
if(!m_trayIcon->isVisible())
return;
QString msg;
for(int i=0; i<messages.length(); i++){
msg.append(messages[i] + "\n");
}
m_trayIcon->showMessage(tr("KanMemo"), msg
, QSystemTrayIcon::Information, 5000);
}
//つぶやく
void TimerDialog::tweetTimerMessage(const QStringList &messages)
{
if(!m_timerdata.tweetFinished())
return;
if(m_settings == NULL)
return;
m_oauth.setConsumerKey(TWITTER_CONSUMER_KEY);
m_oauth.setConsumerSecret(TWITTER_CONSUMER_SECRET);
m_oauth.setToken(m_settings->value(SETTING_GENERAL_TOKEN, "").toString());
m_oauth.setTokenSecret(m_settings->value(SETTING_GENERAL_TOKENSECRET, "").toString());
m_oauth.user_id(m_settings->value(SETTING_GENERAL_USER_ID, "").toString());
m_oauth.screen_name(m_settings->value(SETTING_GENERAL_SCREEN_NAME, "").toString());
int tweet_len_limit = 140;
tweet_len_limit -= m_oauth.screen_name().length() + 2;
tweet_len_limit -= tr("#kanmemo").length();
tweet_len_limit -= 12 + 1 + 1 + 1 + messages.length();
qDebug() << "tweet len limit=" << tweet_len_limit;
if(m_oauth.state() == OAuth::Authorized){
QString message;
if(messages.length() > 0){
//キューに登録したいメッセージがあれば登録
message = "@" + m_oauth.screen_name() + " ";
for(int i=0; i<messages.length(); i++){
QString temp = messages[i];
if((message.length() + temp.length() + 1) >= tweet_len_limit){
//文字数オーバー一旦送信
message.append(QStringLiteral("%1 %2")
.arg(QDateTime::currentDateTime().toString(QStringLiteral("MM/dd hh:mm")))
.arg(tr("#kanmemo")));
// qDebug() << message.length() << "," << message;
//リストに貯めこむ
//qDebug() << "tweet1:" << message;
m_tweetMessageList.append(message);
//消す
message = "@" + m_oauth.screen_name() + " ";
}
//連結
message.append(temp + "\n ");
}
message.append(QStringLiteral("%1 %2")
.arg(QDateTime::currentDateTime().toString(QStringLiteral("MM/dd hh:mm")))
.arg(tr("#kanmemo")));
// qDebug() << message.length() << "," << message;
//リストに貯めこむ
//qDebug() << "tweet2:" << message;
m_tweetMessageList.append(message);
}
if(m_status.loading()){
qDebug() << "now tweeting (timer dialog)";
}else if(!m_tweetMessageList.isEmpty()){
//リストから取り出してつぶやく
qDebug() << "tweet:" << m_tweetMessageList.first();
QVariantMap map;
map.insert("status", m_tweetMessageList.first());
m_status.statusesUpdate(map);
m_tweetMessageList.removeFirst(); //消す
}else{
qDebug() << "tweet list empty";
}
}
}
void TimerDialog::loadSettings()
{
if(m_settings == NULL)
return;
//設定読み込み
m_settings->beginGroup(QStringLiteral(SETTING_TIMER));
//入渠
m_timerdata.setDockingTime(TimerData::toRealList(m_settings->value(QStringLiteral(SETTING_TIMER_DOCKING_TIME), QList<QVariant>() << 0 << 0 << 0 << 0).toList()));
m_timerdata.setDockingStart(TimerData::toRealList(m_settings->value(QStringLiteral(SETTING_TIMER_DOCKING_START), QList<QVariant>() << 0 << 0 << 0 << 0).toList()));
m_timerdata.setDockingRunning(TimerData::toBoolList(m_settings->value(QStringLiteral(SETTING_TIMER_DOCKING_RUNNING), QList<QVariant>() << 0 << 0 << 0 << 0).toList()));
//遠征
m_timerdata.setExpeditionTime(TimerData::toRealList(m_settings->value(QStringLiteral(SETTING_TIMER_EXPEDITION_TIME), QList<QVariant>() << 0 << 0 << 0).toList()));
m_timerdata.setExpeditionStart(TimerData::toRealList(m_settings->value(QStringLiteral(SETTING_TIMER_EXPEDITION_START), QList<QVariant>() << 0 << 0 << 0).toList()));
m_timerdata.setExpeditionRunning(TimerData::toBoolList(m_settings->value(QStringLiteral(SETTING_TIMER_EXPEDITION_RUNNING), QList<QVariant>() << 0 << 0 << 0).toList()));
//建造
m_timerdata.setConstructionTime(TimerData::toRealList(m_settings->value(QStringLiteral(SETTING_TIMER_CONSTRUCTION_TIME), QList<QVariant>() << 0 << 0 << 0 << 0).toList()));
m_timerdata.setConstructionStart(TimerData::toRealList(m_settings->value(QStringLiteral(SETTING_TIMER_CONSTRUCTION_START), QList<QVariant>() << 0 << 0 << 0 << 0).toList()));
m_timerdata.setConstructionRunning(TimerData::toBoolList(m_settings->value(QStringLiteral(SETTING_TIMER_CONSTRUCTION_RUNNING), QList<QVariant>() << 0 << 0 << 0 << 0).toList()));
//つぶやくか
m_timerdata.setTweetFinished(m_settings->value(QStringLiteral(SETTING_TIMER_TWEETFINISHED), true).toBool());
//アラームの設定(暫定で固定)
#ifdef Q_OS_MAC
m_timerdata.setAlarmSoundPath(QString("%1/../../../alarm.mp3")
.arg(QCoreApplication::applicationDirPath()));
#else
m_timerdata.setAlarmSoundPath(QString("%1/alarm.mp3")
.arg(QCoreApplication::applicationDirPath()));
#endif
m_timerdata.setAlarmSoundVolume(0.4);
//折りたたみ
m_timerdata.setDockingClose(m_settings->value(QStringLiteral(SETTING_TIMER_DOCKING_CLOSE), false).toBool());
m_timerdata.setExpeditionClose(m_settings->value(QStringLiteral(SETTING_TIMER_EXPEDITION_CLOSE), false).toBool());
m_timerdata.setConstructionClose(m_settings->value(QStringLiteral(SETTING_TIMER_CONSTRUCTION_CLOSE), false).toBool());
m_settings->endGroup();
//ウインドウの位置を復元
m_settings->beginGroup(QStringLiteral(SETTING_TIMERDIALOG));
restoreGeometry(m_settings->value(QStringLiteral(SETTING_WINDOW_GEO)).toByteArray());
m_settings->endGroup();
}
void TimerDialog::saveSettings()
{
if(m_settings == NULL)
return;
//保存
m_settings->beginGroup(QStringLiteral(SETTING_TIMER));
//入渠
m_settings->setValue(QStringLiteral(SETTING_TIMER_DOCKING_TIME), TimerData::toList<QVariant, qreal>(m_timerdata.dockingTime()));
m_settings->setValue(QStringLiteral(SETTING_TIMER_DOCKING_START), TimerData::toList<QVariant, qreal>(m_timerdata.dockingStart()));
m_settings->setValue(QStringLiteral(SETTING_TIMER_DOCKING_RUNNING), TimerData::toList<QVariant, bool>(m_timerdata.dockingRunning()));
//遠征
m_settings->setValue(QStringLiteral(SETTING_TIMER_EXPEDITION_TIME), TimerData::toList<QVariant, qreal>(m_timerdata.expeditionTime()));
m_settings->setValue(QStringLiteral(SETTING_TIMER_EXPEDITION_START), TimerData::toList<QVariant, qreal>(m_timerdata.expeditionStart()));
m_settings->setValue(QStringLiteral(SETTING_TIMER_EXPEDITION_RUNNING), TimerData::toList<QVariant, bool>(m_timerdata.expeditionRunning()));
//建造
m_settings->setValue(QStringLiteral(SETTING_TIMER_CONSTRUCTION_TIME), TimerData::toList<QVariant, qreal>(m_timerdata.constructionTime()));
m_settings->setValue(QStringLiteral(SETTING_TIMER_CONSTRUCTION_START), TimerData::toList<QVariant, qreal>(m_timerdata.constructionStart()));
m_settings->setValue(QStringLiteral(SETTING_TIMER_CONSTRUCTION_RUNNING), TimerData::toList<QVariant, bool>(m_timerdata.constructionRunning()));
//つぶやくか
m_settings->setValue(QStringLiteral(SETTING_TIMER_TWEETFINISHED), m_timerdata.tweetFinished());
//折りたたみ
m_settings->setValue(QStringLiteral(SETTING_TIMER_DOCKING_CLOSE), m_timerdata.dockingClose());
m_settings->setValue(QStringLiteral(SETTING_TIMER_EXPEDITION_CLOSE), m_timerdata.expeditionClose());
m_settings->setValue(QStringLiteral(SETTING_TIMER_CONSTRUCTION_CLOSE), m_timerdata.constructionClose());
m_settings->endGroup();
//ウインドウの位置を保存
m_settings->beginGroup(QStringLiteral(SETTING_TIMERDIALOG));
m_settings->setValue(QStringLiteral(SETTING_WINDOW_GEO), saveGeometry());
m_settings->endGroup();
}
void TimerDialog::closeEvent(QCloseEvent *event)
{
//設定保存
saveSettings();
QDialog::closeEvent(event);
}