Debian Codegraph @

src/af/xap/gtk/hildon/xap_UnixHildonApp.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 1998 AbiSource, Inc.
5 * Copyright (C) 2005 INdT
6 * Author: Renato Araujo <[email protected]>
7 *
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
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.
18 *
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., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301 USA.
23 */
24
25
26 #include <stdio.h>
27
28 #include "ev_EditMethod.h"
29 #include "xap_UnixHildonApp.h"
30 #include "xap_Frame.h"
31 #include "xap_UnixFrameImpl.h"
32 #include "xad_Document.h"
33
34 // something wrong with these headers -- they need to be included after our own
35 // header
36 #include <log-functions.h>
37 #include <hildon/hildon-program.h>
38
39 bool XAP_UnixHildonApp::s_bInitDone = false;
40 bool XAP_UnixHildonApp::s_bRestoreNeeded = false;
41
42 static void s_topmost_changed_cb (HildonProgram *program,
43 GParamSpec *property_param,
44 gpointer data);
45
46 static void osso_hw_event_cb (osso_hw_state_t *state, gpointer data);
47 static gint osso_rpc_event_cb (const gchar *interface,
48 const gchar *method,
49 GArray *arguments,
50 gpointer data,
51 osso_rpc_t *retval);
52
53 /*****************************************************************/
54 XAP_UnixHildonApp::XAP_UnixHildonApp(const char * szAppName)
55 : XAP_UnixApp(szAppName),
56 m_pOsso(NULL),
57 m_pHildonProgram(NULL),
58 m_bHibernate(false)
59 {
60 }
61
62 XAP_UnixHildonApp::~XAP_UnixHildonApp()
63 {
64 if (m_pOsso)
65 {
66 /* Unset callbacks */
67 osso_hw_unset_event_cb (m_pOsso, NULL);
68 osso_rpc_unset_default_cb_f (m_pOsso, osso_rpc_event_cb, NULL);
69
70 /* Deinit osso */
71 osso_deinitialize (m_pOsso);
72 m_pOsso = NULL;
73 }
74 }
75
76 bool XAP_UnixHildonApp::initialize(const char * szKeyBindingsKey,
77 const char * szKeyBindingsDefaultValue)
78 {
79 osso_return_t ret;
80
81 osso_log (LOG_INFO, "Initializing osso");
82
83 //Initialize osso, for receive hardware signals
84
85 // This prevents AbiWord being started from gdb. To start it from gdb in
86 // Scratchbox you have todo
87 //
88 // $ run-standalone.sh gdb /var/lib/install/usr/bin/AbiWord-2.6
89 m_pOsso = osso_initialize ("com.abisource.abiword",
90 VERSION,
91 FALSE,
92 NULL);
93
94 if (m_pOsso == NULL)
95 {
96 osso_log (LOG_ERR, "Osso initialization failed" );
97 UT_DEBUGMSG(("Osso initialization failed\n"));
98 return false;
99 }
100
101 UT_DEBUGMSG(("m_pOsso 0x%p\n", m_pOsso));
102
103 // Set handling changes in HW states.
104 ret = osso_hw_set_event_cb (m_pOsso, NULL, osso_hw_event_cb, this);
105 if (ret != OSSO_OK)
106 {
107 osso_log (LOG_ERR, "Could not set callback for HW monitoring");
108 }
109
110 ret = osso_rpc_set_default_cb_f(m_pOsso, osso_rpc_event_cb, this);
111 if (ret != OSSO_OK)
112 {
113 osso_log (LOG_ERR, "Could not set callback for receiving messages");
114 }
115
116 return XAP_UnixApp::initialize(szKeyBindingsKey,szKeyBindingsDefaultValue);
117 }
118
119 /*!
120 Some of our dbus messages arrive before the app initialization is completed
121 (in particular the message that tells us to restore from hibernation), and
122 these need to be queued up for later. This function gets called from
123 ap_UnixApp::main() just before gtk_main().
124 */
125 void XAP_UnixHildonApp::processStartupQueue()
126 {
127 UT_DEBUGMSG(("\n$$$$$$$$$$$ XAP_UnixHildonApp::precessStartupQueue() $$$$$$$$$$$$\n"));
128 UT_return_if_fail( s_bInitDone );
129
130 if(s_bRestoreNeeded)
131 {
132 UT_DEBUGMSG(("Restoring state\n"));
133 retrieveState();
134 s_bRestoreNeeded = false;
135 setHibernate(false);
136 }
137 }
138
139
140 /*!
141 This methods returns an instance of HildonProgram object; there is only one
142 instance of it for the applicaiton, and it is created by this function when
143 it is first called.
144 */
145 GObject * XAP_UnixHildonApp::getHildonProgram() const
146 {
147 if(!m_pHildonProgram)
148 {
149 m_pHildonProgram = G_OBJECT (hildon_program_get_instance ());
150 UT_return_val_if_fail (m_pHildonProgram, NULL);
151
152 g_object_set_data(m_pHildonProgram, "user_data",
153 const_cast<XAP_UnixHildonApp*>(this));
154
155 g_signal_connect (m_pHildonProgram, "notify::is-topmost",
156 G_CALLBACK (s_topmost_changed_cb),
157 const_cast<XAP_UnixHildonApp*>(this));
158 }
159
160 return m_pHildonProgram;
161 }
162
163
164 bool XAP_UnixHildonApp::_saveState(XAP_StateData & sd)
165 {
166 osso_state_t osd;
167 osd.state_size = sizeof(XAP_StateData);
168 osd.state_data = (gpointer)& sd;
169
170 osso_return_t ret = osso_state_write(m_pOsso, &osd);
171
172 UT_DEBUGMSG(("Save state called: ret %d\n", ret));
173 return ( ret == OSSO_OK );
174 }
175
176 bool XAP_UnixHildonApp::_retrieveState(XAP_StateData & sd)
177 {
178 /* we use the size of the stored data to differentiate between reall and stale state
179 data (since the state data survives after the app exits normally) */
180
181 char c = 0;
182 osso_state_t osd;
183 osd.state_size = sizeof(char);
184 osd.state_data = (gpointer)& c;
185
186 osso_return_t ret = osso_state_read(m_pOsso, & osd);
187
188 if(ret == OSSO_OK)
189 {
190 // this is not proper data ...
191 return false;
192 }
193
194 // now try the real thing
195 osd.state_size = sizeof(XAP_StateData);
196 osd.state_data = (gpointer)& sd;
197
198 ret = osso_state_read(m_pOsso, & osd);
199
200 UT_DEBUGMSG(("Retrieve state called: ret %d\n", ret));
201 return ( ret == OSSO_OK );
202 }
203
204 void XAP_UnixHildonApp::clearStateInfo()
205 {
206 char c = 0;
207 osso_state_t osd;
208 osd.state_size = sizeof(char);
209 osd.state_data = (gpointer)& c;
210
211 osso_return_t ret = osso_state_write(m_pOsso, &osd);
212 UT_ASSERT_HARMLESS( ret == OSSO_OK );
213 }
214
215 bool XAP_UnixHildonApp::isTopmost(void) const
216 {
217 return hildon_program_get_is_topmost (HILDON_PROGRAM (m_pHildonProgram));
218 }
219
220 static void
221 _topmost_lose (XAP_UnixHildonApp *app,
222 HildonProgram *program)
223 {
224 UT_DEBUGMSG(("%%%%%%%%%%%%%% topmost-status-lose %%%%%%%%%%%%%%%%%%\n"));
225
226 // because it takes us something like 9s to start up, we will only
227 // hibernated if the m_bHibernate flag is set. We set this flag in response
228 // to low memory signal and clear it every time we get awaken from
229 // hibernation
230 if(app->getHibernate())
231 {
232 app->saveState(false);
233 hildon_program_set_can_hibernate (HILDON_PROGRAM (program), TRUE);
234 }
235 }
236
237 static void
238 _topmost_acquire (XAP_UnixHildonApp *app,
239 HildonProgram *program)
240 {
241 UT_DEBUGMSG(("%%%%%%%%%%%%%% topmost-status-acquire %%%%%%%%%%%%%%%%%%\n"));
242 const char * pUntitled = "Untitled%d";
243 const XAP_StringSet * pSS = app->getStringSet();
244 if(pSS)
245 {
246 const char * p = pSS->getValue(XAP_STRING_ID_UntitledDocument);
247 if(p && *p)
248 pUntitled = p;
249 }
250
251 for(UT_sint32 i = 0; i < app->getFrameCount(); ++i)
252 {
253 XAP_Frame * pFrame = app->getFrame(i);
254 if(!pFrame)
255 {
256 UT_ASSERT_HARMLESS( UT_SHOULD_NOT_HAPPEN );
257 continue;
258 }
259
260 AD_Document * pDoc = pFrame->getCurrentDoc();
261 if(!pDoc)
262 {
263 UT_ASSERT_HARMLESS( UT_SHOULD_NOT_HAPPEN );
264 continue;
265 }
266
267 const char * pName = pFrame->getFilename();
268 if(pName && *pName)
269 {
270 // for now, only do this for untitled docs use the localised name
271 // for Untitled and compare len-2 chars (there is %d at the end of
272 // the string)
273 if(!strncmp(pName, pUntitled, strlen(pUntitled)-2))
274 {
275 const char * p = strstr(pName, "HIBERNATED.abw");
276
277 // this was an untitled doc before we lost focus; make it look
278 // like it still is
279 if(p)
280 {
281 pDoc->clearFilename();
282 pDoc->forceDirty();
283 pFrame->updateTitle();
284 }
285 }
286 }
287 }
288
289 // it would be better to do this in conditional fashion, after the user
290 // modified the document, but this will do for now
291 hildon_program_set_can_hibernate (HILDON_PROGRAM (program), FALSE);
292 }
293
294 static void
295 s_topmost_changed_cb (HildonProgram *program,
296 GParamSpec * /*property_param*/,
297 gpointer data)
298 {
299 XAP_UnixHildonApp *pApp;
300 UT_return_if_fail(data != NULL);
301 pApp = static_cast<XAP_UnixHildonApp *>(data);
302
303 if (hildon_program_get_is_topmost (program))
304 _topmost_lose(pApp, program);
305 else
306 _topmost_acquire(pApp, program);
307 }
308
309
310 /* Depending on the state of hw, do something */
311 static void
312 osso_hw_event_cb (osso_hw_state_t *state,
313 gpointer data)
314 {
315 UT_DEBUGMSG(("osso_hw_event_cb() called\n"));
316 XAP_UnixHildonApp *pApp;
317
318 UT_return_if_fail (data != NULL);
319
320 pApp = static_cast<XAP_UnixHildonApp *>(data);
321
322 //signal shutdown received
323 if (state->shutdown_ind) {
324 for (UT_sint32 ndx = 0; ndx < pApp->getFrameCount(); ndx++)
325 pApp->getFrame(ndx)->close();
326 }
327
328 //signal save unsaved data received
329 if (state->save_unsaved_data_ind) {
330 if(XAP_UnixHildonApp::s_bInitDone)
331 {
332 UT_DEBUGMSG(("App ready, proceeding to save state ...\n"));
333 pApp->saveState(false);
334 }
335 else
336 {
337 UT_DEBUGMSG(("App not ready, no state to save ...\n"));
338 }
339 }
340
341 //signal memory low received
342 if (state->memory_low_ind)
343 {
344 UT_DEBUGMSG(("Low memory hw signal\n"));
345 if(XAP_UnixHildonApp::s_bInitDone)
346 {
347 UT_DEBUGMSG(("App ready, proceeding to save state ...\n"));
348 pApp->setHibernate(true);
349
350 // if we are not the active application, save state
351 if(!pApp->isTopmost())
352 {
353 pApp->saveState(false);
354 }
355 }
356 else
357 {
358 UT_DEBUGMSG(("App not ready, no state to save ...\n"));
359 }
360 }
361
362
363 //signal system inactivity received
364 if (state->system_inactivity_ind)
365 {
366 UT_DEBUGMSG(("System inactivity hw signal\n"));
367 }
368
369 }
370
371 static gint osso_rpc_event_cb (const gchar *interface,
372 const gchar *method,
373 GArray * /*arguments*/,
374 gpointer data,
375 osso_rpc_t * /*retval*/)
376 {
377 UT_DEBUGMSG(("\n+++++++++ osso_rpc_event_cb() called; interface %s, method %s +++++++++++++++\n",
378 interface, method));
379
380 XAP_UnixHildonApp *pApp;
381 UT_return_val_if_fail (data != NULL, OSSO_ERROR);
382
383 pApp = static_cast<XAP_UnixHildonApp *>(data);
384
385 // this function can get called before the initialisation process is
386 // finished, so some of these events need to be queued up
387 if(!strcmp(method, "restored"))
388 {
389 if(XAP_UnixHildonApp::s_bInitDone)
390 {
391 UT_DEBUGMSG(("App ready, proceeding to restore state ...\n"));
392 pApp->retrieveState();
393 XAP_UnixHildonApp::s_bRestoreNeeded = false;
394 }
395 else
396 {
397 UT_DEBUGMSG(("App not ready, queuing up state restoration ...\n"));
398 XAP_UnixHildonApp::s_bRestoreNeeded = true;
399 }
400 }
401 else if(!strcmp(method, "top_application"))
402 {
403 if(XAP_UnixHildonApp::s_bInitDone)
404 {
405 UT_DEBUGMSG(("App ready, proceeding to restore state ...\n"));
406 pApp->retrieveState();
407 XAP_UnixHildonApp::s_bRestoreNeeded = false;
408 pApp->setHibernate(false);
409 }
410 else
411 {
412 UT_DEBUGMSG(("App not ready, queuing up state restoration ...\n"));
413 XAP_UnixHildonApp::s_bRestoreNeeded = true;
414 }
415 }
416
417
418 return OSSO_OK;
419 }
420