ngDBusConnection.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN NIGHTINGALE GPL
4 //
5 // This file is part of the Nightingale web player.
6 //
7 // http://getnightingale.com
8 //
9 // This file may be licensed under the terms of of the
10 // GNU General Public License Version 2 (the "GPL").
11 //
12 // Software distributed under the License is distributed
13 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
14 // express or implied. See the GPL for the specific language
15 // governing rights and limitations.
16 //
17 // You should have received a copy of the GPL along with this
18 // program. If not, go to http://www.gnu.org/licenses/gpl.html
19 // or write to the Free Software Foundation, Inc.,
20 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 //
22 // END NIGHTINGALE GPL
23 
24 //
25 */
26 
27 /*
28  * Written by Logan F. Smyth Š 2009
29  * http://logansmyth.com
30  * me@logansmyth.com
31  */
32 
33 #include "ngDBusConnection.h"
34 #include <sbDBus.h>
35 
36 #include <cstring>
37 
38 #include <nsXPCOM.h>
39 #include <nsComponentManagerUtils.h>
40 #include <nsStringAPI.h>
41 #include <nsIMutableArray.h>
42 #include <nsISupportsPrimitives.h>
43 #include <nsITimer.h>
44 
45 #include "prlog.h"
46 #include "prprf.h"
47 
52 #ifdef PR_LOGGING
53  static PRLogModuleInfo* gDBusConnectionLog = nsnull;
54  #define LOG(args) PR_LOG(gDBusConnectionLog, PR_LOG_WARN, args)
55 #else
56  #define LOG(args) /* nothing */
57 #endif /* PR_LOGGING */
58 
59 using namespace std;
60 
61 /* Implementation file */
63 
65 : signal_msg(NULL)
66 , mHandler(nsnull)
67 {
68  /* member initializers and constructor code */
69  #ifdef PR_LOGGING
70  if(!gDBusConnectionLog)
71  gDBusConnectionLog = PR_NewLogModule("ngDBusConnection");
72  #endif
73 }
74 
76 {
77  NS_IF_RELEASE(mHandler);
78 }
79 
80 NS_IMETHODIMP ngDBusConnection::Observe(nsISupports* aSubject, const char* aTopic, const PRUnichar* aData)
81 {
82  nsresult rv = NS_OK;
83  if(!strcmp(aTopic, NS_TIMER_CALLBACK_TOPIC)) {
84  rv = this->Check();
85  NS_ENSURE_SUCCESS(rv, rv);
86  }
87  return rv;
88 }
89 
90 /* long init (in string name); */
91 NS_IMETHODIMP ngDBusConnection::Init(const char *aName)
92 {
93  NS_ENSURE_ARG_POINTER(aName);
94  nsresult rv = nsnull;
95 
96  LOG(("Initializing DBus"));
97 
98  rv = sbDBusConnection::New(getter_Transfers(mConn), DBUS_BUS_SESSION, NULL, NULL, NULL);
99  NS_ENSURE_SUCCESS(rv, rv);
100 
101  sbDBusError error;
102 
103  // Get a bus connection.
104  dbus_bus_request_name(mConn->get(), aName, 0, &error);
105  SB_DBUS_ENSURE_SUCCESS(error, NS_ERROR_FAILURE);
106 
107  LOG(("Flushing"));
108 
109  dbus_connection_flush(mConn->get());
110 
111  LOG(("Setting up Timer"));
112 
113  mTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
114  NS_ENSURE_SUCCESS(rv, rv);
115  rv = mTimer->Init(this, 100, nsITimer::TYPE_REPEATING_SLACK);
116  NS_ENSURE_SUCCESS(rv, rv);
117 
118  LOG(("End Init()"));
119 
120  return rv;
121 }
122 
123 /* long setMatch (in string match); */
124 NS_IMETHODIMP ngDBusConnection::SetMatch(const char *match)
125 {
126  NS_ENSURE_ARG_POINTER(match);
127 
128  sbDBusError error;
129  LOG(("Setting match %s", match));
130 
131  /* listening to messages from all objects as no path is specified */
132  dbus_bus_add_match (mConn->get(), match, &error);
133  SB_DBUS_ENSURE_SUCCESS(error, NS_ERROR_FAILURE);
134 
135 
136  dbus_connection_flush(mConn->get());
137 
138  LOG(("DBus Connection Init"));
139 
140  return NS_OK;
141 }
142 
143 /* void check (); */
144 NS_IMETHODIMP ngDBusConnection::Check()
145 {
146  NS_ENSURE_TRUE(mConn, NS_OK);
147 
148  nsresult rv = NS_OK;
149 
150  DBusMessage* msg;
151  DBusMessage* reply;
152  DBusMessageIter *reply_iter;
153 
154  // non blocking read of the next available message
155  dbus_connection_read_write(mConn->get(), 0);
156  msg = dbus_connection_pop_message(mConn->get());
157 
158  if(msg != NULL){
159  if(dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_METHOD_CALL || dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_SIGNAL){
160  LOG(("*****Calling %s", dbus_message_get_member(msg)));
161 
162  reply_iter = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
163 
164  dbus_message_iter_init(msg, &incoming_args);
165 
166  reply = dbus_message_new_method_return(msg);
167  dbus_message_iter_init_append(reply, reply_iter);
168 
169  outgoing_args.push_back(reply_iter);
170 
171 
172  const char* interface = dbus_message_get_interface(msg);
173  const char* path = dbus_message_get_path(msg);
174  const char* member = dbus_message_get_member(msg);
175 
176  #ifdef PR_LOGGING
177  if(interface != nsnull) LOG(("Interface: %s", interface));
178  if(path != nsnull) LOG(("Path: %s", path));
179  if(member != nsnull) LOG(("Member: %s", member));
180  #endif
181 
182  NS_ENSURE_TRUE(mHandler, NS_ERROR_FAILURE);
183 
184  rv = mHandler->HandleMethod(interface, path, member);
185  NS_ENSURE_SUCCESS(rv, rv);
186 
187  LOG(("Just after Handler call"));
188 
189  dbus_uint32_t serial = 0;
190 
191  // send the reply && flush the connection
192  NS_ENSURE_TRUE(dbus_connection_send(mConn->get(), reply, &serial), NS_ERROR_OUT_OF_MEMORY);
193 
194  while(!outgoing_args.empty()){
195  NS_Free(outgoing_args.back());
196  outgoing_args.pop_back();
197  }
198 
199  dbus_connection_flush(mConn->get());
200  dbus_message_unref(reply);
201 
202 
203  LOG(("Done Handler call"));
204  }
205 
206  dbus_message_unref(msg);
207 
208  }
209 
210  return rv;
211 }
212 
213 /* long end (); */
214 NS_IMETHODIMP ngDBusConnection::End(PRInt32 *_retval)
215 {
216  mTimer->Cancel();
217  return NS_OK;
218 }
219 
220 /* vois prepareMethodCall(in string dest, in string path, in string inter, in string name); */
221 NS_IMETHODIMP ngDBusConnection::PrepareMethodCall(const char* dest, const char* path, const char* inter, const char* name)
222 {
223  NS_ENSURE_ARG_POINTER(dest);
224  NS_ENSURE_ARG_POINTER(path);
225  NS_ENSURE_ARG_POINTER(inter);
226  NS_ENSURE_ARG_POINTER(name);
227 
228  LOG(("*****Preparing method call %s, %s, %s", path, inter, name));
229  DBusMessageIter* reply_iter = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
230 
231  // Create a D-Bus method call message.
232  signal_msg = dbus_message_new_method_call(dest,
233  path,
234  inter,
235  name);
236  dbus_message_iter_init_append(signal_msg, reply_iter);
237  outgoing_args.push_back(reply_iter);
238 
239  return NS_OK;
240 }
241 
242 /* void sendMethodCall (); */
243 NS_IMETHODIMP ngDBusConnection::SendMethodCall()
244 {
245  LOG(("Starting to send method call"));
246 
247  dbus_connection_send(mConn->get(), signal_msg, NULL);
248 
249  dbus_message_unref(signal_msg);
250 
251  LOG(("Freeing args"));
252  while(!outgoing_args.empty()){
253  NS_Free(outgoing_args.back());
254  outgoing_args.pop_back();
255  }
256 
257  dbus_connection_flush(mConn->get());
258 
259  LOG(("*****Method Call Sent"));
260 
261  return NS_OK;
262 }
263 
264 /* void sprepareSignal (in string path, in string inter, in string name); */
265 NS_IMETHODIMP ngDBusConnection::PrepareSignal(const char *path, const char *inter, const char *name)
266 {
267  NS_ENSURE_ARG_POINTER(path);
268  NS_ENSURE_ARG_POINTER(inter);
269  NS_ENSURE_ARG_POINTER(name);
270 
271  LOG(("*****Preparing signal %s, %s, %s", path, inter, name));
272  signal_msg = dbus_message_new_signal(path, inter, name);
273 
274  DBusMessageIter* reply_iter = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
275 
276  dbus_message_iter_init_append(signal_msg, reply_iter);
277  outgoing_args.push_back(reply_iter);
278 
279  LOG(("Signal prepared"));
280 
281  return NS_OK;
282 }
283 
284 /* void sendSignal (); */
285 NS_IMETHODIMP ngDBusConnection::SendSignal()
286 {
287  LOG(("Starting to send signal"));
288 
289  dbus_connection_send(mConn->get(), signal_msg, NULL);
290 
291  dbus_message_unref(signal_msg);
292 
293  LOG(("Freeing args"));
294  while(!outgoing_args.empty()){
295  NS_Free(outgoing_args.back());
296  outgoing_args.pop_back();
297  }
298 
299  dbus_connection_flush(mConn->get());
300 
301  LOG(("*****Signal Sent"));
302 
303  return NS_OK;
304 }
305 /* long setMethodHandler (in ngIMethodHandler handler); */
306 NS_IMETHODIMP ngDBusConnection::SetMethodHandler(ngIMethodHandler* handler)
307 {
308  NS_ENSURE_ARG_POINTER(handler);
309  // we shouldn't need to addref, nor are we allowed, as handler is our parent
310  // by design, but else the pointer gets freed by XPCOM.
311  handler->AddRef();
312  this->mHandler = handler;
313  return NS_OK;
314 }
315 
316 /* long getInt64Arg (); */
317 NS_IMETHODIMP ngDBusConnection::GetInt64Arg(PRInt64 *_retval)
318 {
319  int type = dbus_message_iter_get_arg_type(&incoming_args);
320 
321  if(type == DBUS_TYPE_INT64){
322  dbus_int64_t data;
323  dbus_message_iter_get_basic(&incoming_args, &data);
324  dbus_message_iter_next(&incoming_args);
325 
326  *_retval = data;
327  }
328  else{
329  char* msg = PR_smprintf("WARNING: %i received but expecting a Int64", type);
330  NS_WARNING(msg);
331  PR_smprintf_free(msg);
332  }
333 
334  return NS_OK;
335 }
336 
337 /* long getBoolArg (); */
338 NS_IMETHODIMP ngDBusConnection::GetBoolArg(PRBool *_retval)
339 {
340  int type = dbus_message_iter_get_arg_type(&incoming_args);
341 
342  if(type == DBUS_TYPE_BOOLEAN){
343  dbus_bool_t data;
344  dbus_message_iter_get_basic(&incoming_args, &data);
345  dbus_message_iter_next(&incoming_args);
346 
347  *_retval = data;
348  }
349  else{
350  char* msg = PR_smprintf("WARNING: %i received but expecting a Bool", type);
351  NS_WARNING(msg);
352  PR_smprintf_free(msg);
353  }
354 
355  return NS_OK;
356 }
357 
358 /* long getStringArg (); */
359 NS_IMETHODIMP ngDBusConnection::GetStringArg(char **_retval)
360 {
361 
362  int type = dbus_message_iter_get_arg_type(&incoming_args);
363 
364  if(type == DBUS_TYPE_STRING){
365  const char* dat;
366  dbus_message_iter_get_basic(&incoming_args, &dat);
367 
368  //this is going to leak
369  //not sure if the garbage collector will get this or not...
370  *_retval = (char*)NS_Alloc(sizeof(char)*(strlen(dat) + 1));
371 
372  strcpy(*_retval, dat);
373  LOG(("String contained %s", *_retval));
374 
375  dbus_message_iter_next(&incoming_args);
376 
377  }
378  else{
379  char* msg = PR_smprintf("WARNING: %i received but expecting a String", type);
380  NS_WARNING(msg);
381  PR_smprintf_free(msg);
382  }
383 
384  return NS_OK;
385 }
386 
387 /* long getObjectPathArg (); */
388 NS_IMETHODIMP ngDBusConnection::GetObjectPathArg(char **_retval)
389 {
390 
391  int type = dbus_message_iter_get_arg_type(&incoming_args);
392 
393  if(type == DBUS_TYPE_OBJECT_PATH){
394  const char* dat;
395  dbus_message_iter_get_basic(&incoming_args, &dat);
396 
397  //this is going to leak
398  //not sure if the garbage collector will get this or not...
399  *_retval = (char*)NS_Alloc(sizeof(char)*(strlen(dat) + 1));
400 
401  strcpy(*_retval, dat);
402 
403  dbus_message_iter_next(&incoming_args);
404 
405  }
406  else{
407  char* msg = PR_smprintf("WARNING: %i received but expecting a Object", type);
408  NS_WARNING(msg);
409  PR_smprintf_free(msg);
410  }
411 
412  return NS_OK;
413 }
414 
415 /* double getInt64Arg (); */
416 NS_IMETHODIMP ngDBusConnection::GetDoubleArg(PRFloat64 *_retval)
417 {
418  int type = dbus_message_iter_get_arg_type(&incoming_args);
419 
420  if(type == DBUS_TYPE_DOUBLE){
421  double data;
422  dbus_message_iter_get_basic(&incoming_args, &data);
423  dbus_message_iter_next(&incoming_args);
424 
425  *_retval = data;
426  }
427  else{
428  char* msg = PR_smprintf("WARNING: %i received but expecting a Double", type);
429  NS_WARNING(msg);
430  PR_smprintf_free(msg);
431  }
432 
433  return NS_OK;
434 }
435 
436 /* nsIArray getArrayArg (); */
437 NS_IMETHODIMP ngDBusConnection::GetArrayArg(nsIArray** _retval)
438 {
439  LOG(("Getting array argument"));
440 
441  nsresult rv;
442  nsCOMPtr<nsIMutableArray> array = do_CreateInstance("@mozilla.org/array;1", &rv);
443  NS_ENSURE_SUCCESS(rv, rv);
444  int type = dbus_message_iter_get_arg_type(&incoming_args);
445 
446  if(type == DBUS_TYPE_ARRAY){
447  DBusMessageIter data;
449  int type;
450  dbus_message_iter_recurse(&incoming_args, &data);
451  while(dbus_message_iter_has_next(&data)) {
452  dbus_message_iter_get_basic(&data, &value);
453  type = dbus_message_iter_get_arg_type(&data);
454  dbus_message_iter_next(&data);
455 
456  if(type == DBUS_TYPE_STRING || type == DBUS_TYPE_OBJECT_PATH) {
457  // Convert the DBusBasicValue to something that implements nsISupports
458  nsCOMPtr<nsISupportsString> stringVal = do_CreateInstance("@mozilla.org/supports-string;1", &rv);
459  NS_ENSURE_SUCCESS(rv, rv);
460 
461  rv = stringVal->SetData(NS_ConvertUTF8toUTF16(NS_LITERAL_CSTRING(value.string)));
462  NS_ENSURE_SUCCESS(rv, rv);
463 
464  rv = array->AppendElement(stringVal, false);
465  NS_ENSURE_SUCCESS(rv, rv);
466  }
467  else {
468  rv = NS_ERROR_NOT_IMPLEMENTED;
469  }
470  }
471 
472  dbus_message_iter_next(&incoming_args);
473 
474  *_retval = array;
475  NS_ADDREF(*_retval);
476  }
477  else{
478  char* msg = PR_smprintf("WARNING: %i received but expecting an Array", type);
479  NS_WARNING(msg);
480  PR_smprintf_free(msg);;
481  }
482 
483  LOG(("Array argument got"));
484 
485  return rv;
486 }
487 
488 /* void setInt32Arg (in long val); */
489 NS_IMETHODIMP ngDBusConnection::SetInt32Arg(PRInt32 val)
490 {
491  LOG(("Setting Int32 %d", val));
492  DBusMessageIter* args = outgoing_args.back();
493 
494  dbus_int32_t data = val;
495  dbus_message_iter_append_basic(args, DBUS_TYPE_INT32, &data);
496 
497  LOG(("Set Int32"));
498 
499  return NS_OK;
500 }
501 
502 
503 /* void setUInt32Arg (in long val); */
504 NS_IMETHODIMP ngDBusConnection::SetUInt32Arg(PRUint32 val)
505 {
506  LOG(("Setting uInt32 %d", val));
507 
508  DBusMessageIter* args = outgoing_args.back();
509 
510  dbus_message_iter_append_basic(args, DBUS_TYPE_UINT32, &val);
511 
512  LOG(("Set uInt32"));
513 
514  return NS_OK;
515 }
516 
517 /* void setUInt16Arg (in long val); */
518 NS_IMETHODIMP ngDBusConnection::SetUInt16Arg(PRUint16 val)
519 {
520  LOG(("Set uInt16 %d", val));
521  DBusMessageIter* args = outgoing_args.back();
522 
523  dbus_uint16_t data = val;
524  dbus_message_iter_append_basic(args, DBUS_TYPE_UINT16, &data);
525 
526  LOG(("Set uInt16"));
527  return NS_OK;
528 }
529 
530 /* void setStringArg (in string val); */
531 //NS_IMETHODIMP ngDBusConnection::SetStringArg(const nsAString &data)
532 NS_IMETHODIMP ngDBusConnection::SetStringArg(const nsAString& val)
533 {
534  DBusMessageIter* args = outgoing_args.back();
535 
536  NS_ConvertUTF16toUTF8_external val8(val);
537  const char* data = val8.BeginReading();
538  LOG(("Setting string %s", data));
539 
540  dbus_message_iter_append_basic(args, DBUS_TYPE_STRING, &data);
541 
542  LOG(("Set string"));
543  return NS_OK;
544 }
545 
546 /* void setDictSSEntryArg (in string key, in AString val, [optional] in boolean escape); */
547 NS_IMETHODIMP ngDBusConnection::SetDictSSEntryArg(const char *key, const nsAString &val)
548 {
549  NS_ENSURE_ARG_POINTER(key);
550 
551  DBusMessageIter* array_obj = outgoing_args.back();
552  DBusMessageIter entry_obj;
553  DBusMessageIter var_obj;
554 
555  NS_ConvertUTF16toUTF8_external val8(val);
556  const char* data = val8.BeginReading();
557 
558  LOG(("Setting dict SS %s:%s", key, data));
559 
560  dbus_message_iter_open_container(array_obj, DBUS_TYPE_DICT_ENTRY, NULL, &entry_obj);
561  dbus_message_iter_append_basic(&entry_obj, DBUS_TYPE_STRING, &key);
562 
563  dbus_message_iter_open_container(&entry_obj, DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &var_obj);
564  dbus_message_iter_append_basic(&var_obj, DBUS_TYPE_STRING, &data);
565  dbus_message_iter_close_container(&entry_obj, &var_obj);
566  dbus_message_iter_close_container(array_obj, &entry_obj);
567 
568  LOG(("Set dict SS entry"));
569 
570  return NS_OK;
571 }
572 
573 /* void setDictSOEntryArg (in string key, in AString val, [optional] in boolean escape); */
574 NS_IMETHODIMP ngDBusConnection::SetDictSOEntryArg(const char *key, const nsAString& val)
575 {
576  NS_ENSURE_ARG_POINTER(key);
577 
578  DBusMessageIter* array_obj = outgoing_args.back();
579  DBusMessageIter entry_obj;
580  DBusMessageIter var_obj;
581 
582  NS_ConvertUTF16toUTF8_external val8(val);
583  const char* data = val8.BeginReading();
584 
585  LOG(("Setting dict SO %s:%s", key, data));
586 
587  dbus_message_iter_open_container(array_obj, DBUS_TYPE_DICT_ENTRY, NULL, &entry_obj);
588  dbus_message_iter_append_basic(&entry_obj, DBUS_TYPE_STRING, &key);
589 
590  dbus_message_iter_open_container(&entry_obj, DBUS_TYPE_VARIANT, DBUS_TYPE_OBJECT_PATH_AS_STRING, &var_obj);
591  dbus_message_iter_append_basic(&var_obj, DBUS_TYPE_OBJECT_PATH, &data);
592  dbus_message_iter_close_container(&entry_obj, &var_obj);
593  dbus_message_iter_close_container(array_obj, &entry_obj);
594 
595  LOG(("Set dict SO entry"));
596 
597  return NS_OK;
598 }
599 
600 /* void setDictSIEntryArg (in string key, in long long val); */
601 NS_IMETHODIMP ngDBusConnection::SetDictSI64EntryArg(const char *key, PRInt64 val)
602 {
603  NS_ENSURE_ARG_POINTER(key);
604  LOG(("Setting dict SI64 %s:%d", key, val));
605 
606  DBusMessageIter* array_obj = outgoing_args.back();
607  DBusMessageIter entry_obj;
608  DBusMessageIter var_obj;
609 
610  dbus_message_iter_open_container(array_obj, DBUS_TYPE_DICT_ENTRY, NULL, &entry_obj);
611  dbus_message_iter_append_basic(&entry_obj, DBUS_TYPE_STRING, &key);
612 
613  dbus_message_iter_open_container(&entry_obj, DBUS_TYPE_VARIANT, DBUS_TYPE_INT64_AS_STRING, &var_obj);
614  dbus_message_iter_append_basic(&var_obj, DBUS_TYPE_INT64, &val);
615  dbus_message_iter_close_container(&entry_obj, &var_obj);
616  dbus_message_iter_close_container(array_obj, &entry_obj);
617 
618  LOG(("Set dict SI64 entry"));
619 
620  return NS_OK;
621 }
622 
623 /* void setDictSIEntryArg (in string key, in long val); */
624 NS_IMETHODIMP ngDBusConnection::SetDictSIEntryArg(const char *key, PRUint32 val)
625 {
626  NS_ENSURE_ARG_POINTER(key);
627  LOG(("Setting dict SI %s:%d", key, val));
628 
629  DBusMessageIter* array_obj = outgoing_args.back();
630  DBusMessageIter entry_obj;
631  DBusMessageIter var_obj;
632 
633  dbus_message_iter_open_container(array_obj, DBUS_TYPE_DICT_ENTRY, NULL, &entry_obj);
634  dbus_message_iter_append_basic(&entry_obj, DBUS_TYPE_STRING, &key);
635 
636  dbus_message_iter_open_container(&entry_obj, DBUS_TYPE_VARIANT, DBUS_TYPE_UINT32_AS_STRING, &var_obj);
637  dbus_message_iter_append_basic(&var_obj, DBUS_TYPE_UINT32, &val);
638  dbus_message_iter_close_container(&entry_obj, &var_obj);
639  dbus_message_iter_close_container(array_obj, &entry_obj);
640 
641  LOG(("Set dict SI entry"));
642 
643  return NS_OK;
644 }
645 
646 /* void setDictSBEntryArg (in string key, in boolean val); */
647 NS_IMETHODIMP ngDBusConnection::SetDictSBEntryArg(const char *key, PRBool val)
648 {
649  NS_ENSURE_ARG_POINTER(key);
650  LOG(("Setting dict SB %s:%i", key, val));
651 
652  DBusMessageIter* array_obj = outgoing_args.back();
653  DBusMessageIter entry_obj;
654  DBusMessageIter var_obj;
655 
656  dbus_message_iter_open_container(array_obj, DBUS_TYPE_DICT_ENTRY, NULL, &entry_obj);
657  dbus_message_iter_append_basic(&entry_obj, DBUS_TYPE_STRING, &key);
658 
659  dbus_message_iter_open_container(&entry_obj, DBUS_TYPE_VARIANT, DBUS_TYPE_BOOLEAN_AS_STRING, &var_obj);
660  dbus_message_iter_append_basic(&var_obj, DBUS_TYPE_BOOLEAN, &val);
661  dbus_message_iter_close_container(&entry_obj, &var_obj);
662  dbus_message_iter_close_container(array_obj, &entry_obj);
663 
664  LOG(("Set dict SB entry"));
665 
666  return NS_OK;
667 }
668 
669 /* void setDictSDEntryArg (in string key, in double val); */
670 NS_IMETHODIMP ngDBusConnection::SetDictSDEntryArg(const char *key, PRFloat64 val)
671 {
672  NS_ENSURE_ARG_POINTER(key);
673  LOG(("Setting dict SD %s:%f", key, val));
674 
675  DBusMessageIter* array_obj = outgoing_args.back();
676  DBusMessageIter entry_obj;
677  DBusMessageIter var_obj;
678 
679  dbus_message_iter_open_container(array_obj, DBUS_TYPE_DICT_ENTRY, NULL, &entry_obj);
680  dbus_message_iter_append_basic(&entry_obj, DBUS_TYPE_STRING, &key);
681 
682  double data = val;
683  dbus_message_iter_open_container(&entry_obj, DBUS_TYPE_VARIANT, DBUS_TYPE_DOUBLE_AS_STRING, &var_obj);
684  dbus_message_iter_append_basic(&var_obj, DBUS_TYPE_DOUBLE, &data);
685  dbus_message_iter_close_container(&entry_obj, &var_obj);
686  dbus_message_iter_close_container(array_obj, &entry_obj);
687 
688  LOG(("Set dict SD entry"));
689 
690  return NS_OK;
691 }
692 
693 /* void openDictSAEntryArg (in string key); */
694 NS_IMETHODIMP ngDBusConnection::OpenDictSAEntryArg(const char *key, PRInt16 aType = TYPE_STRING)
695 {
696  NS_ENSURE_ARG_POINTER(key);
697  LOG(("Opening dict SA : %s", key));
698 
699  nsresult rv;
700  DBusMessageIter* array_obj = outgoing_args.back();
701  DBusMessageIter* entry_obj = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
702 
703  DBusMessageIter* new_val = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));;
704 
705  dbus_message_iter_open_container(array_obj, DBUS_TYPE_DICT_ENTRY, NULL, entry_obj);
706  dbus_message_iter_append_basic(entry_obj, DBUS_TYPE_STRING, &key);
707 
708  char combinedType[strlen(DBUS_TYPE_ARRAY_AS_STRING) + strlen(this->ngTypeToDBusType(aType)) + 1];
709  combinedType[0] = '\0';
710  strcat(combinedType, DBUS_TYPE_ARRAY_AS_STRING);
711  strcat(combinedType, this->ngTypeToDBusType(aType));
712 
713  dbus_message_iter_open_container(entry_obj, DBUS_TYPE_VARIANT, combinedType, new_val);
714 
715  outgoing_args.push_back(entry_obj);
716  outgoing_args.push_back(new_val);
717 
718  rv = this->OpenArray(aType);
719  NS_ENSURE_SUCCESS(rv, rv);
720 
721  LOG(("Opened dict SA entry"));
722 
723  return rv;
724 }
725 
726 /* void closeDictSAEntryArg (); */
727 NS_IMETHODIMP ngDBusConnection::CloseDictSAEntryArg()
728 {
729  LOG(("Closing dict SA "));
730 
731  nsresult rv;
732  rv = this->CloseArray();
733  NS_ENSURE_SUCCESS(rv, rv);
734 
735  DBusMessageIter* new_val = outgoing_args.back();
736  outgoing_args.pop_back();
737  DBusMessageIter* entry_obj = outgoing_args.back();
738  outgoing_args.pop_back();
739  DBusMessageIter* array_obj = outgoing_args.back();
740 
741  dbus_message_iter_close_container(entry_obj, new_val);
742  dbus_message_iter_close_container(array_obj, entry_obj);
743 
744  LOG(("Closed dict SA entry"));
745 
746  return rv;
747 }
748 /* void openDictSDEntryArg (in string key); */
749 NS_IMETHODIMP ngDBusConnection::OpenDictSDEntryArg(const char *key)
750 {
751  NS_ENSURE_ARG_POINTER(key);
752  LOG(("Opening dict SD :%s", key));
753 
754  DBusMessageIter* array_obj = outgoing_args.back();
755  DBusMessageIter* entry_obj = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
756 
757  DBusMessageIter* new_val = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
758  DBusMessageIter* new_arr = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
759 
760  dbus_message_iter_open_container(array_obj, DBUS_TYPE_DICT_ENTRY, NULL, entry_obj);
761  dbus_message_iter_append_basic(entry_obj, DBUS_TYPE_STRING, &key);
762 
763  dbus_message_iter_open_container(entry_obj, DBUS_TYPE_VARIANT, DBUS_TYPE_ARRAY_AS_STRING DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING, new_val);
764  dbus_message_iter_open_container(new_val, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING, new_arr);
765 
766  outgoing_args.push_back(entry_obj);
767  outgoing_args.push_back(new_val);
768  outgoing_args.push_back(new_arr);
769 
770  LOG(("Opened dict SD entry"));
771 
772  return NS_OK;
773 }
774 
775 /* void closeDictSDEntryArg (); */
776 NS_IMETHODIMP ngDBusConnection::CloseDictSDEntryArg()
777 {
778  LOG(("Closing dict SD "));
779 
780  DBusMessageIter* new_arr = outgoing_args.back();
781  outgoing_args.pop_back();
782  DBusMessageIter* new_val = outgoing_args.back();
783  outgoing_args.pop_back();
784  DBusMessageIter* entry_obj = outgoing_args.back();
785  outgoing_args.pop_back();
786  DBusMessageIter* array_obj = outgoing_args.back();
787 
788  dbus_message_iter_close_container(new_val, new_arr);
789  dbus_message_iter_close_container(entry_obj, new_val);
790  dbus_message_iter_close_container(array_obj, entry_obj);
791 
792  LOG(("Closed dict SD entry"));
793 
794  return NS_OK;
795 }
796 
797 
798 /* void setBoolArg (in bool val); */
799 NS_IMETHODIMP ngDBusConnection::SetBoolArg(PRBool val)
800 {
801  LOG(("Setting Bool %i", val));
802  DBusMessageIter* args = outgoing_args.back();
803 
804  dbus_bool_t data = val;
805  dbus_message_iter_append_basic(args, DBUS_TYPE_BOOLEAN, &data);
806 
807  LOG(("Set Bool"));
808 
809  return NS_OK;
810 }
811 
812 /* void setDoubleArg (in double val); */
813 NS_IMETHODIMP ngDBusConnection::SetDoubleArg(PRFloat64 val)
814 {
815  LOG(("Set Double %f", val));
816  DBusMessageIter* args = outgoing_args.back();
817 
818  double data = val;
819  dbus_message_iter_append_basic(args, DBUS_TYPE_DOUBLE, &data);
820 
821  LOG(("Set Double"));
822  return NS_OK;
823 }
824 
825 /* void setInt64Arg (in long long val); */
826 NS_IMETHODIMP ngDBusConnection::SetInt64Arg(PRInt64 val)
827 {
828  LOG(("Set Int64 %d", val));
829  DBusMessageIter* args = outgoing_args.back();
830 
831  dbus_int64_t data = val;
832  dbus_message_iter_append_basic(args, DBUS_TYPE_INT64, &data);
833 
834  LOG(("Set Int64"));
835  return NS_OK;
836 }
837 
838 /* void setArrayStringArg (in string key, in long val); */
839 NS_IMETHODIMP ngDBusConnection::SetArrayStringArg(const nsAString& val)
840 {
841  return this->SetStringArg(val);
842 }
843 
844 /* void setObjectPathArg (in string val); */
845 //NS_IMETHODIMP ngDBusConnection::SetObjectPathArg(const nsAString &val)
846 NS_IMETHODIMP ngDBusConnection::SetObjectPathArg(const char *data)
847 {
848  NS_ENSURE_ARG_POINTER(data);
849  DBusMessageIter* args = outgoing_args.back();
850 
851  LOG(("Setting object path %s", data));
852 
853  dbus_message_iter_append_basic(args, DBUS_TYPE_OBJECT_PATH, &data);
854 
855  LOG(("Set object path"));
856  return NS_OK;
857 }
858 
859 /* void openDictEntryArray (); */
860 NS_IMETHODIMP ngDBusConnection::OpenDictEntryArray()
861 {
862  LOG(("Opening dict entry"));
863 
864  DBusMessageIter* args = outgoing_args.back();
865  DBusMessageIter* new_args = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
866 
867  dbus_message_iter_open_container(args, DBUS_TYPE_ARRAY, DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING, new_args);
868 
869  outgoing_args.push_back(new_args);
870 
871  LOG(("Dict entry open"));
872  return NS_OK;
873 }
874 
875 /* void closeDictEntryArray (); */
876 NS_IMETHODIMP ngDBusConnection::CloseDictEntryArray()
877 {
878  LOG(("Closing dict entry"));
879 
880  DBusMessageIter* new_args = outgoing_args.back();
881  outgoing_args.pop_back();
882  DBusMessageIter* args = outgoing_args.back();
883 
884  dbus_message_iter_close_container(args, new_args);
885 
886  LOG(("Dict entry closed"));
887  return NS_OK;
888 }
889 
890 /* void openArray (); */
891 NS_IMETHODIMP ngDBusConnection::OpenArray(PRInt16 aType = TYPE_STRING)
892 {
893  LOG(("Opening array"));
894 
895  DBusMessageIter* args = outgoing_args.back();
896  DBusMessageIter* new_args = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
897 
898  dbus_message_iter_open_container(args, DBUS_TYPE_ARRAY, this->ngTypeToDBusType(aType), new_args);
899 
900  outgoing_args.push_back(new_args);
901 
902  LOG(("Array open"));
903  return NS_OK;
904 }
905 
906 /* void closeArray (); */
907 NS_IMETHODIMP ngDBusConnection::CloseArray()
908 {
909  LOG(("Closing array"));
910 
911  DBusMessageIter* new_args = outgoing_args.back();
912  outgoing_args.pop_back();
913  DBusMessageIter* args = outgoing_args.back();
914 
915  dbus_message_iter_close_container(args, new_args);
916 
917  LOG(("Array closed"));
918  return NS_OK;
919 }
920 
921 /* void openStruct (); */
922 NS_IMETHODIMP ngDBusConnection::OpenStruct()
923 {
924  LOG(("Opening struct"));
925 
926  DBusMessageIter* args = outgoing_args.back();
927  DBusMessageIter* new_args = (DBusMessageIter*)NS_Alloc(sizeof(DBusMessageIter));
928 
929  dbus_message_iter_open_container(args, DBUS_TYPE_STRUCT, NULL, new_args);
930 
931  outgoing_args.push_back(new_args);
932  LOG(("Struct opened"));
933 
934  return NS_OK;
935 }
936 
937 /* void closeStruct (); */
938 NS_IMETHODIMP ngDBusConnection::CloseStruct()
939 {
940  LOG(("Closing struct"));
941  DBusMessageIter* new_args = outgoing_args.back();
942  outgoing_args.pop_back();
943  DBusMessageIter* args = outgoing_args.back();
944 
945  dbus_message_iter_close_container(args, new_args);
946  LOG(("Struct closed"));
947 
948  return NS_OK;
949 }
950 
951 const char* ngDBusConnection::ngTypeToDBusType(const int ngType) const
952 {
953  if(ngType == TYPE_OBJECT_PATH) {
954  return DBUS_TYPE_OBJECT_PATH_AS_STRING;
955  }
956 
957  return DBUS_TYPE_STRING_AS_STRING;
958 }
static nsresult New(sbDBusConnection **aConnection, DBusBusType aBusType, const char *aDestination, const char *aPath, const char *aInterface)
Definition: sbDBus.cpp:157
var args
Definition: alert.js:8
return NS_OK
inArray array
#define LOG(args)
NS_IMPL_ISUPPORTS2(sbAlbumArtService, sbIAlbumArtService, nsIObserver) NS_IMETHODIMP sbAlbumArtService
this _dialogInput val(dateText)
function Init()
ExtensionSchemeMatcher prototype match
virtual ~ngDBusConnection()
Songbird D-Bus Service Definitions.
_updateCookies aName
countRef value
Definition: FeedWriter.js:1423
#define SB_DBUS_ENSURE_SUCCESS(aError, aReturnValue)
Definition: sbDBus.h:326
function msg
observe data
Definition: FeedWriter.js:1329
GstMessage gpointer data sbGStreamerMessageHandler * handler
_updateTextAndScrollDataForFrame aData