forked from ioriayane/KanmusuMemory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimerdialog.cpp
More file actions
302 lines (254 loc) · 11.7 KB
/
Copy pathtimerdialog.cpp
File metadata and controls
302 lines (254 loc) · 11.7 KB
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
/*
* Copyright 2013 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 "../KanmusuMemoryTwitter/twitterinfo.h"
#include <QQmlContext>
#include <QtQml/QQmlEngine>
#include <QDateTime>
#include <QtCore/QDebug>
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()));
loadSettings();
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);
if(m_viewer == NULL){
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->setSource(QUrl("qrc:///qml/KanmusuMemory/timerDialog.qml"));
m_viewer->show();
}
}
void TimerDialog::closeQml()
{
// accept();
}
//全体のカウント処理
void TimerDialog::timeout()
{
QStringList messages;
QStringList numbername;
numbername << tr("1st") << tr("2nd") << tr("3rd") << tr("4th") << tr("5th") << tr("6th") << tr("7th") << tr("8th");
//入渠
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);
//メッセージ追加
messages.append(tr("It is time that Kanmusu is up from the %1 bath.").arg(numbername[i]));
// messages.append(tr("ドック%1の艦娘がお風呂から上がる頃です。").arg(i+1));
}
}
}
//遠征
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);
//メッセージ追加
messages.append(tr("It is time the %1 Fleet is returned.").arg(numbername[i+1]));
}
}
}
//建造
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);
//メッセージ追加
messages.append(tr("It is time the Kanmusu of %1 dock is completed.").arg(numbername[i]));
}
}
}
if(messages.length() > 0){
//システムトレイ
showTimerMessage(messages);
//つぶやく
tweetTimerMessage(messages);
}
}
//時間が来ているかチェックする
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());
if(m_oauth.state() == OAuth::Authorized){
qDebug() << "認証済み";
QString message;
message = "@" + m_oauth.screen_name() + " ";
for(int i=0; i<messages.length(); i++){
QString temp = messages[i];
if((message.length() + temp.length()) >= 120){
//文字数オーバー一旦送信
message.append(QStringLiteral("%1").arg(QDateTime::currentDateTime().toString(QStringLiteral("MM/dd hh:mm"))));
// qDebug() << message.length() << "," << message;
QVariantMap map;
map.insert("status", message);
m_status.statusesUpdate(map);
//消す
message = "@" + m_oauth.screen_name() + " ";
message.append(temp + "\n ");
}else{
message.append(temp + "\n ");
}
}
message.append(QStringLiteral("%1").arg(QDateTime::currentDateTime().toString(QStringLiteral("MM/dd hh:mm"))));
// qDebug() << message.length() << "," << message;
QVariantMap map;
map.insert("status", message);
m_status.statusesUpdate(map);
}
}
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_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->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);
}