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
32 #include "sipe-common.h"
33 #include "http-conn.h" /* sipe-cal.h requires this */
34 #include "sipe-backend.h"
36 #include "sipe-core.h"
37 #include "sipe-core-private.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"
46 #define SIPE_IDLE_SET_DELAY 1 /* seconds */
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
;
88 sip
->status
= g_strdup(status_id
);
91 void sipe_status_set_activity(struct sipe_core_private
*sipe_private
,
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
);
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
;
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
,
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]",
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
);
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
,
147 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
148 struct sipe_account_data
*sip
= SIPE_ACCOUNT_DATA_PRIVATE
;
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
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.");
177 sipe_status_set_token(sipe_private
, status_id
);
179 /* hack to escape apostrof before comparison */
180 tmp
= note
? sipe_utils_str_replace(note
, "'", "'") : NULL
;
182 /* this will preserve OOF flag as well */
183 if (!sipe_strequal(tmp
, sip
->note
)) {
184 SIPE_CORE_PRIVATE_FLAG_UNSET(OOF_NOTE
);
186 sip
->note
= g_strdup(note
);
187 sip
->note_since
= time(NULL
);
191 /* schedule 2 sec to capture idle flag */
192 action_name
= g_strdup("<+set-status>");
193 sipe_schedule_seconds(sipe_private
,
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
;
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");
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
;
232 sip
->idle_switch
= time(NULL
);
233 SIPE_DEBUG_INFO("sipe_core_status_idle: sip->idle_switch : %s",
234 asctime(localtime(&(sip
->idle_switch
))));