-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediator.cpp
100 lines (88 loc) · 2.38 KB
/
mediator.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
#include <QFile>
#include <QStandardPaths>
#include <QDir>
#include <QTextStream>
#include "mediator.h"
Mediator::Mediator(QObject *parent) : QObject(parent)
{
_AlarmModel = new Alarmmodel();
path = QStandardPaths::standardLocations(QStandardPaths::DataLocation).value(0);
QDir dir(path);
if (!dir.exists())
dir.mkpath(path);
if (!path.isEmpty() && !path.endsWith("/"))
path += "/";
path+="alarms.txt";
qDebug(path.toLatin1());
QFile file(path);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
while (!in.atEnd())
{
QString hour,minutes,title_song,day,status;
hour=in.readLine();
minutes=in.readLine();
title_song=in.readLine();
day=in.readLine();
status=in.readLine();
if(hour.length()==0)
{
break;
}
_AlarmModel->addAlarm(Alarm(hour,minutes,title_song,day,status));
}
file.close();
}
}
void Mediator::updateFile()
{
int i;
QFile file(path);
int rCount = _AlarmModel->rowCount();
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream out(&file);
for (i=0;i< rCount;i++){
out<<_AlarmModel->getHour(i)<<endl<< _AlarmModel->getMinutes(i)<<endl<< _AlarmModel->getTitleSong(i)<<endl<< _AlarmModel->getDay(i)<<endl<< _AlarmModel->getStatus(i)<<endl;
}
file.close();
}
}
int Mediator::count()
{
return _AlarmModel->rowCount();
}
void Mediator::removeAlarm(int index){
_AlarmModel->removeAlarm(index);
AlarmModelChanged();
updateFile();
}
void Mediator::insertAlarm(QString hour,QString minutes,QString title_song,QString day,QString status){
_AlarmModel->addAlarm(Alarm(hour,minutes,title_song,day, status));
AlarmModelChanged();
updateFile();
}
QString Mediator::getHour(int i)
{
return _AlarmModel->getHour(i);
}
QString Mediator::getMinutes(int i)
{
return _AlarmModel->getMinutes(i);
}
QString Mediator::getTitleSong(int i){
return _AlarmModel->getTitleSong(i);
}
QString Mediator::getDay(int i){
return _AlarmModel->getDay(i);
}
QString Mediator::getStatus(int i){
return _AlarmModel->getStatus(i);
}
void Mediator::setStatus(int i, QString s)
{
_AlarmModel->setStatus(i,s);
AlarmModelChanged();
updateFile();
}