Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar.c
blob: a3cd78bd55475b4a6432f28fd61d3b318dd11f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
  sugar.c

  Sugar DE integration

  Copyright (C) 2010, Aleksey Lim

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  (See COPYING.txt)
*/

#include <sys/stat.h>
#include <unistd.h>
#include <SDL/SDL_syswm.h>

#include <polyol/env.h>
#include <polyol/ds.h>
#include <polyol/shell.h>

#include "sugar.h"

static void chooser_response_cb(GObject*, const gchar*);

static SugarShell *shell;
static SugarJournal *journal;
static SugarJobject *jobject;
static gboolean new_jobject;
static GMainLoop *chooser_loop;
static char *chosen_object_id;

void sugar_setup(int * argc, char *** argv)
{
  g_type_init();

  if (!sugar_init(argv, argc))
    return;

  g_debug("Use sugar mode");

  sugar_environ_set_sync_dbus(TRUE);

  if (sugar_environ_get_object_id() != NULL)
    jobject = sugar_jobject_find(sugar_environ_get_object_id());

  if (jobject != NULL)
    g_debug("Journal entry %s was found", sugar_environ_get_object_id());
  else
  {
    jobject = sugar_jobject_create();
    g_debug("Create new Journal entry");
  }

  shell = sugar_shell_new();

  journal = sugar_journal_new();
  g_signal_connect(journal, "chooser-response",
          G_CALLBACK(chooser_response_cb), NULL);

  chooser_loop = g_main_loop_new (NULL, FALSE);
}

int sugar_detected()
{
  return sugar_environ_get_initialized();
}

void sugar_cleanup()
{
  if (!sugar_detected())
    return;

  g_debug("Cleanup sugar mode");

  if (new_jobject)
    sugar_journal_show_object(journal, sugar_jobject_get_uid(jobject));

  g_object_unref(G_OBJECT(jobject));
  jobject = NULL;

  g_object_unref(G_OBJECT(shell));
  shell = NULL;

  g_object_unref(G_OBJECT(journal));
  journal = NULL;

  g_main_loop_unref(chooser_loop);
  chooser_loop = NULL;
}

void sugar_setup_x11(int * window_width, int * window_height)
{
  if (!sugar_detected())
    return;

  SDL_SysWMinfo wminfo;
  SDL_VERSION(&wminfo.version);

  if (SDL_GetWMInfo(&wminfo))
  {
    Display * display = wminfo.info.x11.display;
    Window window = wminfo.info.x11.wmwindow;

    wminfo.info.x11.lock_func();

    sugar_environ_set_window(display, window);
    *window_width = DisplayWidth(display, DefaultScreen(display));
    *window_height = DisplayHeight(display, DefaultScreen(display));

    wminfo.info.x11.unlock_func();
  }
  else
  {
      g_warning ("Cannot get WMInfo to setup X11 properties");
  }
}

const char *sugar_get_savedir(void)
{
  return sugar_environ_get_activity_root();
}

char *sugar_get_image_file()
{
  if (!sugar_detected())
    return NULL;

  const char * file_path = sugar_jobject_get_file_path(jobject);

  if (file_path != NULL)
    g_debug("Load image from journal entry %s", sugar_jobject_get_uid(jobject));

  return g_strdup(file_path);
}

void sugar_save_image(char *path)
{
  if (!sugar_detected())
    return;

  if (sugar_jobject_get_uid(jobject) == NULL)
      new_jobject |= TRUE;

  sugar_jobject_set_mime_type(jobject, "image/png");
  sugar_jobject_write_file(jobject, path, TRUE);

  g_debug("Saved %s image to journal entry %s", path,
          sugar_jobject_get_uid(jobject));
}

void sugar_save_thumb(char *path)
{
  struct stat fstat;
  g_return_if_fail(stat(path, &fstat) == 0);

  FILE *fp = fopen(path, "rb");
  g_return_if_fail(fp);

  GArray *preview = g_array_new(FALSE, FALSE, 1);
  g_array_set_size(preview, fstat.st_size);

  if (fread(preview->data, 1, fstat.st_size, fp) == (size_t) fstat.st_size)
  {
    sugar_jobject_set_preview(jobject, preview);
    sugar_jobject_write(jobject);
  }

  fclose(fp);
  unlink(path);
}

const char *sugar_open()
{
  sugar_journal_choose_object(journal, SUGAR_MIME_IMAGE);
  g_main_loop_run(chooser_loop);

  if (chosen_object_id == NULL)
    return NULL;

  SugarJobject *image_jobject = sugar_jobject_find(chosen_object_id);
  g_free(chosen_object_id);
  chosen_object_id = NULL;
  g_return_val_if_fail(image_jobject, NULL);

  const char *image_path = sugar_jobject_get_file_path(image_jobject);
  g_return_val_if_fail(image_path, NULL);

  return g_strdup(image_path);
}

static void chooser_response_cb(GObject *sender, const gchar *object_id)
{
  g_debug("SugarShell.chooser_response_cb=%s", object_id);
  chosen_object_id = g_strdup(object_id);
  g_main_loop_quit(chooser_loop);
}