New levels, re-structuring for testing
[numtypysics.git] / OsHildon.cpp
blobbf80fbf6cf287973541ae8b92603b0a23bd16569
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.
17 #ifdef USE_HILDON
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>
28 #include <hildon/hildon-program.h>
29 //#include <hildon/hildon-file-chooser-dialog.h>
30 #include <gtk/gtk.h>
32 #include "Os.h"
33 #include "Http.h"
34 #define Font __FONT_REDEF
35 #include "Config.h"
37 #define NP_NAME "NumptyPhysics"
38 #define NP_SERVICE "org.maemo.garage.numptyphysics"
39 #define NP_OBJECT "org/maemo/garage/numptyphysics" /* / ?? */
40 #define NP_INTERFACE "org.maemo.garage.numptyphysics"
41 #define NP_VERSION "1.0"
42 #define MAX_FILES 32
45 static gint dbus_handler(const gchar *interface,
46 const gchar *method,
47 GArray *arguments,
48 gpointer data,
49 osso_rpc_t *retval);
51 class OsHildon : public Os
53 GMainContext *m_gcontext;
54 osso_context_t *m_osso;
56 public:
57 int m_numFiles;
58 char* m_files[MAX_FILES];
60 OsHildon()
62 g_type_init();
63 m_gcontext = g_main_context_new();
65 m_osso = osso_initialize(NP_NAME, NP_VERSION, FALSE, m_gcontext);
66 if (m_osso == NULL) {
67 fprintf(stderr, "Failed to initialize libosso\n");
68 return;
71 /* Set dbus handler to get mime open callbacks */
72 if ( osso_rpc_set_cb_f(m_osso,
73 NP_SERVICE,
74 NP_OBJECT,
75 NP_INTERFACE,
76 dbus_handler, NULL) != OSSO_OK) {
77 fprintf(stderr, "Failed to set mime callback\n");
78 return;
82 ~OsHildon()
84 if ( m_osso ) {
85 osso_deinitialize( m_osso );
87 if ( m_gcontext ) {
88 g_main_context_unref( m_gcontext );
92 virtual void poll()
94 if ( g_main_context_iteration( m_gcontext, FALSE ) ) {
95 fprintf(stderr, "Hildon::poll event!\n");
99 virtual char *getLaunchFile()
101 if ( m_numFiles > 0 ) {
102 return m_files[--m_numFiles];
104 return NULL;
107 virtual bool openBrowser( const char* url )
109 if ( url && strlen(url) < 200 ) {
110 char buf[256];
111 snprintf(buf,256,"xdg-open %s",url);
112 if ( system( buf ) == 0 ) {
113 return true;
116 return false;
119 virtual char* saveDialog( const char* path )
121 #if 0
122 static char buf[256];
123 GtkWidget *dialog = hildon_file_chooser_dialog_new(NULL,GTK_FILE_CHOOSER_ACTION_SAVE);
124 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), path);
125 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
126 gchar *name;
127 name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
128 strncpy( buf, name, 256 );
129 g_print("Saving as %s\n", name);
130 g_free(name);
132 return buf;
133 #else
134 return NULL;
135 #endif
141 static gint dbus_handler(const gchar *interface,
142 const gchar *method,
143 GArray *arguments,
144 gpointer data,
145 osso_rpc_t *retval)
147 OsHildon* os = (OsHildon*)data;
148 if (arguments == NULL) {
149 return OSSO_OK;
152 if (g_ascii_strcasecmp(method, "mime_open") == 0) {
153 for(unsigned i = 0; i < arguments->len; ++i) {
154 osso_rpc_t val = g_array_index(arguments, osso_rpc_t, i);
155 if (val.type == DBUS_TYPE_STRING && val.value.s != NULL) {
156 char *f = NULL;
157 fprintf(stderr,"hildon mime open \"%s\"\n",val.value.s);
158 if ( strncmp(val.value.s,"file://",7)==0
159 && os->m_numFiles < MAX_FILES ) {
160 f = val.value.s+7;
161 } else if ( ( strncmp(val.value.s,"http://",7)==0
162 || strncmp(val.value.s,"nptp://",7)==0 ) ) {
163 Http h;
164 std::string filename(val.value.s+7);
165 if ( filename.rfind('/') >= 0 ) {
166 filename = filename.substr( filename.rfind('/')+1 );
168 std::string filepath = Config::userDataDir() + Os::pathSep;
169 if ( h.get( val.value.s+7, HTTP_TEMP_FILE ) ) {
170 f = HTTP_TEMP_FILE;
173 if ( f ) {
174 if ( os->m_files[os->m_numFiles] ) {
175 g_free(os->m_files[os->m_numFiles]);
177 os->m_files[os->m_numFiles++] = g_strdup( f );
183 return OSSO_OK;
187 Os* Os::get()
189 static OsHildon os;
190 return &os;
193 const char Os::pathSep = '/';
195 int main(int argc, char** argv)
197 gtk_init(&argc, &argv);
198 npmain(argc,argv);
201 #endif