core cleanup: move out is_oof_note flag
[siplcs.git] / src / core / sipe-status.c
blob3f4efdda69b1a90f19befa978b9c8e598b9755b3
1 /**
2 * @file sipe-status.c
4 * pidgin-sipe
6 * Copyright (C) 2011 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #include <time.h>
30 #include <glib.h>
32 #include "sipe-common.h"
33 #include "http-conn.h" /* sipe-cal.h requires this */
34 #include "sipe-backend.h"
35 #include "sipe-cal.h"
36 #include "sipe-core.h"
37 #include "sipe-core-private.h"
38 #include "sipe-nls.h"
39 #include "sipe-ocs2005.h"
40 #include "sipe-ocs2007.h"
41 #include "sipe-schedule.h"
42 #include "sipe-status.h"
43 #include "sipe-utils.h"
44 #include "sipe.h"
46 #define SIPE_IDLE_SET_DELAY 1 /* seconds */
48 static struct
50 guint type;
51 const gchar *desc;
52 } const sipe_activity_map[SIPE_ACTIVITY_NUM_TYPES] = {
54 * This has nothing to do with Availability numbers, like 3500 (online).
55 * Just a mapping of Communicator Activities to translations
57 /* @TODO: NULL means "default translation from Pidgin"?
58 * What about other backends? */
59 { SIPE_ACTIVITY_UNSET, NULL },
60 { SIPE_ACTIVITY_AVAILABLE, NULL },
61 { SIPE_ACTIVITY_ONLINE, NULL },
62 { SIPE_ACTIVITY_INACTIVE, N_("Inactive") },
63 { SIPE_ACTIVITY_BUSY, N_("Busy") },
64 { SIPE_ACTIVITY_BUSYIDLE, N_("Busy-Idle") },
65 { SIPE_ACTIVITY_DND, NULL },
66 { SIPE_ACTIVITY_BRB, N_("Be right back") },
67 { SIPE_ACTIVITY_AWAY, NULL },
68 { SIPE_ACTIVITY_LUNCH, N_("Out to lunch") },
69 { SIPE_ACTIVITY_INVISIBLE, NULL },
70 { SIPE_ACTIVITY_OFFLINE, NULL },
71 { SIPE_ACTIVITY_ON_PHONE, N_("In a call") },
72 { SIPE_ACTIVITY_IN_CONF, N_("In a conference") },
73 { SIPE_ACTIVITY_IN_MEETING, N_("In a meeting") },
74 { SIPE_ACTIVITY_OOF, N_("Out of office") },
75 { SIPE_ACTIVITY_URGENT_ONLY, N_("Urgent interruptions only") }
78 const gchar *sipe_core_activity_description(guint type)
80 return(gettext(sipe_activity_map[type].desc));
83 void sipe_status_set_token(struct sipe_core_private *sipe_private,
84 const gchar *status_id)
86 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
87 g_free(sip->status);
88 sip->status = g_strdup(status_id);
91 void sipe_status_set_activity(struct sipe_core_private *sipe_private,
92 guint activity)
94 sipe_status_set_token(sipe_private,
95 sipe_backend_activity_to_token(activity));
98 void sipe_core_reset_status(struct sipe_core_public *sipe_public)
100 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
101 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
102 sipe_ocs2007_reset_status(sipe_private);
103 else
104 sipe_ocs2005_reset_status(sipe_private);
107 void sipe_status_and_note(struct sipe_core_private *sipe_private,
108 const gchar *status_id)
110 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
112 if (!status_id)
113 status_id = sip->status;
115 SIPE_DEBUG_INFO("sipe_status_and_note: switch to '%s' for the account", status_id);
117 if (sipe_backend_status_and_note(SIPE_CORE_PUBLIC,
118 status_id,
119 sip->note)) {
120 /* status has changed */
121 guint activity = sipe_backend_token_to_activity(status_id);
123 sip->do_not_publish[activity] = time(NULL);
124 SIPE_DEBUG_INFO("sipe_status_and_note: do_not_publish[%s]=%d [now]",
125 status_id,
126 (int) sip->do_not_publish[activity]);
130 void sipe_status_update(struct sipe_core_private *sipe_private,
131 SIPE_UNUSED_PARAMETER gpointer unused)
133 const gchar *status = sipe_backend_status(SIPE_CORE_PUBLIC);
135 if (!status) return;
137 SIPE_DEBUG_INFO("sipe_status_update: status: %s (%s)", status,
138 sipe_status_changed_by_user(sipe_private) ? "USER" : "MACHINE");
140 sipe_cal_presence_publish(sipe_private, FALSE);
143 void sipe_core_status_set(struct sipe_core_public *sipe_public,
144 const gchar *status_id,
145 const gchar *note)
147 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
148 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
150 if (sip) {
151 gchar *action_name;
152 gchar *tmp;
153 time_t now = time(NULL);
154 guint activity = sipe_backend_token_to_activity(status_id);
155 gboolean do_not_publish = ((now - sip->do_not_publish[activity]) <= 2);
157 /* when other point of presence clears note, but we are keeping
158 * state if OOF note.
160 if (do_not_publish && !note && sip->cal && sip->cal->oof_note) {
161 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_status_set: enabling publication as OOF note keepers.");
162 do_not_publish = FALSE;
165 SIPE_DEBUG_INFO("sipe_core_status_set: was: sip->do_not_publish[%s]=%d [?] now(time)=%d",
166 status_id, (int)sip->do_not_publish[activity], (int)now);
168 sip->do_not_publish[activity] = 0;
169 SIPE_DEBUG_INFO("sipe_core_status_set: set: sip->do_not_publish[%s]=%d [0]",
170 status_id, (int)sip->do_not_publish[activity]);
172 if (do_not_publish) {
173 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_status_set: publication was switched off, exiting.");
174 return;
177 sipe_status_set_token(sipe_private, status_id);
179 /* hack to escape apostrof before comparison */
180 tmp = note ? sipe_utils_str_replace(note, "'", "&apos;") : NULL;
182 /* this will preserve OOF flag as well */
183 if (!sipe_strequal(tmp, sip->note)) {
184 SIPE_CORE_PRIVATE_FLAG_UNSET(OOF_NOTE);
185 g_free(sip->note);
186 sip->note = g_strdup(note);
187 sip->note_since = time(NULL);
189 g_free(tmp);
191 /* schedule 2 sec to capture idle flag */
192 action_name = g_strdup("<+set-status>");
193 sipe_schedule_seconds(sipe_private,
194 action_name,
195 NULL,
196 SIPE_IDLE_SET_DELAY,
197 sipe_status_update,
198 NULL);
199 g_free(action_name);
204 * Whether user manually changed status or
205 * it was changed automatically due to user
206 * became inactive/active again
208 gboolean sipe_status_changed_by_user(struct sipe_core_private *sipe_private)
210 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
211 gboolean res;
212 time_t now = time(NULL);
214 SIPE_DEBUG_INFO("sipe_status_changed_by_user: sip->idle_switch : %s",
215 asctime(localtime(&(sip->idle_switch))));
216 SIPE_DEBUG_INFO("sipe_status_changed_by_user: now : %s",
217 asctime(localtime(&now)));
219 res = ((now - SIPE_IDLE_SET_DELAY * 2) >= sip->idle_switch);
221 SIPE_DEBUG_INFO("sipe_status_changed_by_user: res = %s",
222 res ? "USER" : "MACHINE");
223 return res;
226 void sipe_core_status_idle(struct sipe_core_public *sipe_public)
228 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
229 struct sipe_account_data *sip = SIPE_ACCOUNT_DATA_PRIVATE;
231 if (sip) {
232 sip->idle_switch = time(NULL);
233 SIPE_DEBUG_INFO("sipe_core_status_idle: sip->idle_switch : %s",
234 asctime(localtime(&(sip->idle_switch))));
239 Local Variables:
240 mode: c
241 c-file-style: "bsd"
242 indent-tabs-mode: t
243 tab-width: 8
244 End: