19 #include "timeformatter.h"
24 struct TimeFormatterPrivate
26 TimeFormatter *formatter;
32 GDBusConnection *system_bus;
33 guint subscription_id;
34 GCancellable *cancellable;
38 timedate1_properties_changed (GDBusConnection *connection,
39 const gchar *sender_name,
40 const gchar *object_path,
41 const gchar *interface_name,
42 const gchar *signal_name,
47 Q_UNUSED(sender_name);
48 Q_UNUSED(object_path);
49 Q_UNUSED(interface_name);
50 Q_UNUSED(signal_name);
52 TimeFormatterPrivate *priv = (TimeFormatterPrivate *)user_data;
57 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE (
"(sa{sv}as)")))
60 g_variant_get (parameters,
"(s@a{sv}as)",
nullptr, &changed, &iter);
62 if (g_variant_lookup (changed,
"Timezone",
"s",
nullptr)) {
63 priv->formatter->update();
66 while (g_variant_iter_next (iter,
"&s", &name)) {
67 if (g_str_equal (name,
"Timezone")) {
68 priv->formatter->update();
74 g_variant_unref (changed);
75 g_variant_iter_free (iter);
79 got_bus(GObject *
object, GAsyncResult *result, gpointer user_data)
83 TimeFormatterPrivate *priv = (TimeFormatterPrivate *)user_data;
84 GError *error =
nullptr;
86 priv->system_bus = g_bus_get_finish (result, &error);
87 if (priv->system_bus ==
nullptr) {
88 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
89 qWarning(
"TimeFormatter: cannot connect to the system bus: %s", error->message);
100 priv->subscription_id = g_dbus_connection_signal_subscribe (priv->system_bus,
102 "org.freedesktop.DBus.Properties",
105 "org.freedesktop.timedate1",
106 G_DBUS_SIGNAL_FLAGS_NONE,
107 timedate1_properties_changed,
111 TimeFormatter::TimeFormatter(QObject *parent): QObject(parent)
113 priv =
new TimeFormatterPrivate;
114 priv->formatter =
this;
116 priv->format =
"yyyy-MM-dd hh:mm";
117 priv->system_bus =
nullptr;
118 priv->subscription_id = 0;
119 priv->cancellable = g_cancellable_new ();
121 g_bus_get (G_BUS_TYPE_SYSTEM, priv->cancellable, got_bus, priv);
124 TimeFormatter::TimeFormatter(
const QString &initialFormat, QObject *parent): TimeFormatter(parent)
126 priv->format = initialFormat;
129 TimeFormatter::~TimeFormatter()
131 if (priv->system_bus) {
132 g_dbus_connection_signal_unsubscribe (priv->system_bus, priv->subscription_id);
133 g_object_unref (priv->system_bus);
136 g_cancellable_cancel (priv->cancellable);
137 g_object_unref (priv->cancellable);
140 QString TimeFormatter::format()
const
145 QString TimeFormatter::timeString()
const
147 return priv->timeString;
150 qint64 TimeFormatter::time()
const
155 void TimeFormatter::setFormat(
const QString &format)
157 if (priv->format != format) {
158 priv->format = format;
159 Q_EMIT formatChanged(priv->format);
164 void TimeFormatter::setTime(qint64 time)
166 if (priv->time != time) {
168 Q_EMIT timeChanged(priv->time);
173 void TimeFormatter::update()
175 priv->timeString = formatTime();
176 Q_EMIT timeStringChanged(priv->timeString);
179 QString TimeFormatter::formatTime()
const
181 return QDateTime::fromMSecsSinceEpoch(time() / 1000).toString(format());
184 GDateTimeFormatter::GDateTimeFormatter(QObject* parent)
185 : TimeFormatter(
"%d-%m-%Y %I:%M%p", parent)
189 QString GDateTimeFormatter::formatTime()
const
193 QByteArray formatBytes = format().toUtf8();
195 datetime = g_date_time_new_from_unix_local(time());
200 time_string = g_date_time_format(datetime, formatBytes.constData());
201 QString formattedTime(QString::fromUtf8(time_string));
204 g_date_time_unref(datetime);
205 return formattedTime;