Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar.c
blob: 7e5b727d11211a4f4c346e6e2d238e492239732b (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
/*
  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 <SDL/SDL_syswm.h>
#include <sugar/env.h>
#include <sugar/ds.h>
#include <sugar/shell.h>

#include "sugar.h"

static SugarHandle * handle;
static SugarJobject * jobject;
static gboolean new_jobject;

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

  sugar_sys_set_sync_dbus(TRUE);

  handle = sugar_sys_parse_cmd_options(argv, argc);
  if (handle == NULL)
    return;

  g_debug("Use sugar mode");

  if (sugar_handle_get_object_id(handle) != NULL)
    jobject = sugar_jobject_find(sugar_handle_get_object_id(handle));

  if (jobject == NULL)
    jobject = sugar_jobject_create(sugar_handle_get_activity_id(handle));
}

void sugar_cleanup()
{
  g_debug("Cleanup sugar mode");

  if (new_jobject)
    sugar_shell_show_object(sugar_jobject_get_uid(jobject));

  if (jobject != NULL)
  {
    g_object_unref(G_OBJECT(jobject));
    jobject = NULL;
  }

  if (handle != NULL)
  {
    sugar_handle_unref(handle);
    handle = NULL;
  }
}

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

  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_sys_setup_xprops(handle, 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");
  }
}

int sugar_detected()
{
  return jobject != NULL;
}

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

  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_jobject_file(char * path)
{
  if (!sugar_detected())
    return;

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

  sugar_jobject_write_file(jobject, path, TRUE);

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

char * sugar_get_savedir(void)
{
  return g_strdup_printf("%s/data", sugar_profile_get_activity_root());
}