1 /*
2 * This file is part of NumptyPhysics
3 * Copyright (C) 2008 Tim Edmonds
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 */
16
17 #ifdef USE_HILDON
18
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <glib-object.h>
23 #include <glibconfig.h>
24 #include <glib/gmacros.h>
25 #include <libosso.h>
26 #include <ossoemailinterface.h>
27
28 #include <hildon/hildon-program.h>
29 #include <hildon/hildon-file-chooser-dialog.h>
30 #include <gtk/gtk.h>
31
32 #include "Os.h"
33 #include "Config.h"
34 #include "Http.h"
35
36 #define NP_NAME "NumptyPhysics"
37 #define NP_SERVICE "org.maemo.garage.numptyphysics"
38 #define NP_OBJECT "org/maemo/garage/numptyphysics" /* / ?? */
39 #define NP_INTERFACE "org.maemo.garage.numptyphysics"
40 #define NP_VERSION "1.0"
41 #define MAX_FILES 32
42
43
44 static gint dbus_handler(const gchar *interface,
45 const gchar *method,
46 GArray *arguments,
47 gpointer data,
48 osso_rpc_t *retval);
49
50 class OsHildon : public Os
51 {
52 GMainContext *m_gcontext;
53 osso_context_t *m_osso;
54
55 public:
56 int m_numFiles;
57 char* m_files[MAX_FILES];
58
OsHildon()59 OsHildon()
60 {
61 g_type_init();
62 m_gcontext = g_main_context_new();
63
64 m_osso = osso_initialize(NP_NAME, NP_VERSION, FALSE, m_gcontext);
65 if (m_osso == NULL) {
66 fprintf(stderr, "Failed to initialize libosso\n");
67 return;
68 }
69
70 /* Set dbus handler to get mime open callbacks */
71 if ( osso_rpc_set_cb_f(m_osso,
72 NP_SERVICE,
73 NP_OBJECT,
74 NP_INTERFACE,
75 dbus_handler, NULL) != OSSO_OK) {
76 fprintf(stderr, "Failed to set mime callback\n");
77 return;
78 }
79 }
80
~OsHildon()81 ~OsHildon()
82 {
83 if ( m_osso ) {
84 osso_deinitialize( m_osso );
85 }
86 if ( m_gcontext ) {
87 g_main_context_unref( m_gcontext );
88 }
89 }
90
poll()91 virtual void poll()
92 {
93 if ( g_main_context_iteration( m_gcontext, FALSE ) ) {
94 fprintf(stderr, "Hildon::poll event!\n");
95 }
96 }
97
getLaunchFile()98 virtual char *getLaunchFile()
99 {
100 if ( m_numFiles > 0 ) {
101 return m_files[--m_numFiles];
102 }
103 return NULL;
104 }
105
openBrowser(const char * url)106 virtual bool openBrowser( const char* url )
107 {
108 if ( url && strlen(url) < 200 ) {
109 char buf[256];
110 snprintf(buf,256,"xdg-open %s",url);
111 if ( system( buf ) == 0 ) {
112 return true;
113 }
114 }
115 return false;
116 }
117
saveDialog(const char * path)118 virtual char* saveDialog( const char* path )
119 {
120 static char buf[256];
121 GtkWidget *dialog = hildon_file_chooser_dialog_new(NULL,GTK_FILE_CHOOSER_ACTION_SAVE);
122 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), path);
123 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
124 gchar *name;
125 name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
126 strncpy( buf, name, 256 );
127 g_print("Saving as %s\n", name);
128 g_free(name);
129 }
130 return buf;
131 }
132
133 };
134
135
dbus_handler(const gchar * interface,const gchar * method,GArray * arguments,gpointer data,osso_rpc_t * retval)136 static gint dbus_handler(const gchar *interface,
137 const gchar *method,
138 GArray *arguments,
139 gpointer data,
140 osso_rpc_t *retval)
141 {
142 OsHildon* os = (OsHildon*)data;
143 if (arguments == NULL) {
144 return OSSO_OK;
145 }
146
147 if (g_ascii_strcasecmp(method, "mime_open") == 0) {
148 for(unsigned i = 0; i < arguments->len; ++i) {
149 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, i);
150 if (val.type == DBUS_TYPE_STRING && val.value.s != NULL) {
151 char *f = NULL;
152 fprintf(stderr,"hildon mime open \"%s\"\n",val.value.s);
153 if ( strncmp(val.value.s,"file://",7)==0
154 && os->m_numFiles < MAX_FILES ) {
155 f = val.value.s+7;
156 } else if ( ( strncmp(val.value.s,"http://",7)==0
157 || strncmp(val.value.s,"nptp://",7)==0 ) ) {
158 Http h;
159 if ( h.get( val.value.s+7, HTTP_TEMP_FILE ) ) {
160 f = HTTP_TEMP_FILE;
161 }
162 }
163 if ( f ) {
164 if ( os->m_files[os->m_numFiles] ) {
165 g_free(os->m_files[os->m_numFiles]);
166 }
167 os->m_files[os->m_numFiles++] = g_strdup( f );
168 }
169 }
170 }
171 }
172
173 return OSSO_OK;
174 }
175
176
get()177 Os* Os::get()
178 {
179 static OsHildon os;
180 return &os;
181 }
182
183 const char Os::pathSep = '/';
184
main(int argc,char ** argv)185 int main(int argc, char** argv)
186 {
187 gtk_init(&argc, &argv);
188 npmain(argc,argv);
189 }
190
191 #endif
192