Moved location update logic to new class called UpdateLocation.
[situare] / src / engine / updatelocation.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QDebug>
23 #include <QSettings>
24
25 #include "common.h"
26
27 #include "updatelocation.h"
28
29 const QString UNSENT_MESSAGE_SETTING = "UNSENT_MESSAGE";
30 const QString UNSENT_MESSAGE_PUBLISH_SETTING = "UNSENT_MESSAGE_PUBLISH";
31
32 UpdateLocation::UpdateLocation(QObject *parent) :
33     QObject(parent),
34     m_publish(false)
35 {
36     qWarning() << __PRETTY_FUNCTION__;
37
38     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
39     m_message = settings.value(UNSENT_MESSAGE_SETTING).toString();
40     m_publish = settings.value(UNSENT_MESSAGE_PUBLISH_SETTING, false).toBool();
41 }
42
43 UpdateLocation::~UpdateLocation()
44 {
45     qWarning() << __PRETTY_FUNCTION__;
46
47     QSettings settings(SETTINGS_ORGANIZATION_NAME, SETTINGS_APPLICATION_NAME);
48
49     if (!m_message.isEmpty()) {
50         settings.setValue(UNSENT_MESSAGE_SETTING, m_message);
51         settings.setValue(UNSENT_MESSAGE_PUBLISH_SETTING, m_publish);
52     } else {
53         settings.remove(UNSENT_MESSAGE_SETTING);
54         settings.remove(UNSENT_MESSAGE_PUBLISH_SETTING);
55     }
56 }
57
58 void UpdateLocation::clear()
59 {
60     qWarning() << __PRETTY_FUNCTION__;
61
62     m_message.clear();
63     m_publish = false;
64 }
65
66 QString UpdateLocation::message() const
67 {
68     qWarning() << __PRETTY_FUNCTION__;
69
70     return m_message;
71 }
72
73 void UpdateLocation::send()
74 {
75     qWarning() << __PRETTY_FUNCTION__;
76
77     emit locationUpdate(m_message, m_publish);
78 }
79
80 void UpdateLocation::setMessage(const QString &message)
81 {
82     qWarning() << __PRETTY_FUNCTION__;
83
84     if (message != m_message) {
85         m_message = message;
86         emit messageChanged();
87     }
88 }
89
90 bool UpdateLocation::publish() const
91 {
92     qWarning() << __PRETTY_FUNCTION__;
93
94     return m_publish;
95 }
96
97 void UpdateLocation::setPublish(bool publish)
98 {
99     qWarning() << __PRETTY_FUNCTION__;
100
101     if (publish != m_publish) {
102         m_publish = publish;
103         emit publishChanged();
104     }
105 }