summaryrefslogtreecommitdiff
path: root/libslab/libslab-utils.c
blob: 622c99c7ad550ae9e05c511e2974c3b9717f66d7 (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
208
209
210
211
212
#include "libslab-utils.h"

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <gtk/gtk.h>

#define DESKTOP_ITEM_TERMINAL_EMULATOR_FLAG "TerminalEmulator"
#define ALTERNATE_DOCPATH_KEY               "DocPath"

static FILE *checkpoint_file;

MateDesktopItem *
libslab_mate_desktop_item_new_from_unknown_id (const gchar *id)
{
	MateDesktopItem *item;
	gchar            *basename;

	GError *error = NULL;


	if (! id)
		return NULL;

	item = mate_desktop_item_new_from_uri (id, 0, & error);

	if (! error)
		return item;
	else {
		g_error_free (error);
		error = NULL;
	}

	item = mate_desktop_item_new_from_file (id, 0, & error);

	if (! error)
		return item;
	else {
		g_error_free (error);
		error = NULL;
	}

	item = mate_desktop_item_new_from_basename (id, 0, & error);

	if (! error)
		return item;
	else {
		g_error_free (error);
		error = NULL;
	}

	basename = g_strrstr (id, "/");

	if (basename) {
		basename++;

		item = mate_desktop_item_new_from_basename (basename, 0, &error);

		if (! error)
			return item;
		else {
			g_error_free (error);
			error = NULL;
		}
	}

	return NULL;
}

/* Ugh, here we don't have knowledge of the screen that is being used.  So, do
 * what we can to find it.
 */
GdkScreen *
libslab_get_current_screen (void)
{
	GdkEvent *event;
	GdkScreen *screen = NULL;

	event = gtk_get_current_event ();
	if (event) {
		if (event->any.window)
			screen = gtk_window_get_screen (GTK_WINDOW (event->any.window));

		gdk_event_free (event);
	}

	if (!screen)
		screen = gdk_screen_get_default ();

	return screen;
}

guint32
libslab_get_current_time_millis ()
{
	GTimeVal t_curr;

	g_get_current_time (& t_curr);

	return 1000L * t_curr.tv_sec + t_curr.tv_usec / 1000L;
}

gint
libslab_strcmp (const gchar *a, const gchar *b)
{
	if (! a && ! b)
		return 0;

	if (! a)
		return strcmp ("", b);

	if (! b)
		return strcmp (a, "");

	return strcmp (a, b);
}

void
libslab_handle_g_error (GError **error, const gchar *msg_format, ...)
{
	gchar   *msg;
	va_list  args;


	va_start (args, msg_format);
	msg = g_strdup_vprintf (msg_format, args);
	va_end (args);

	if (*error) {
		g_log (
			G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
			"\nGError raised: [%s]\nuser_message: [%s]\n", (*error)->message, msg);

		g_error_free (*error);

		*error = NULL;
	}
	else
		g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "\nerror raised: [%s]\n", msg);

	g_free (msg);
}

static guint thumbnail_factory_idle_id;
static MateDesktopThumbnailFactory *thumbnail_factory;

static void
create_thumbnail_factory (void)
{
	/* The thumbnail_factory may already have been created by an applet
	 * instance that was launched before the current one.
	 */
	if (thumbnail_factory != NULL)
		return;

	libslab_checkpoint ("create_thumbnail_factory(): start");

	thumbnail_factory = mate_desktop_thumbnail_factory_new (MATE_DESKTOP_THUMBNAIL_SIZE_NORMAL);

	libslab_checkpoint ("create_thumbnail_factory(): end");
}

static gboolean
init_thumbnail_factory_idle_cb (gpointer data)
{
	create_thumbnail_factory ();
	thumbnail_factory_idle_id = 0;
	return FALSE;
}

void
libslab_checkpoint (const char *format, ...)
{
	va_list args;
	struct timeval tv;
	struct tm tm;
	struct rusage rusage;

	if (!checkpoint_file)
		return;

	gettimeofday (&tv, NULL);
	tm = *localtime (&tv.tv_sec);

	getrusage (RUSAGE_SELF, &rusage);

	fprintf (checkpoint_file,
		 "%02d:%02d:%02d.%04d (user:%d.%04d, sys:%d.%04d) - ",
		 (int) tm.tm_hour,
		 (int) tm.tm_min,
		 (int) tm.tm_sec,
		 (int) (tv.tv_usec / 100),
		 (int) rusage.ru_utime.tv_sec,
		 (int) (rusage.ru_utime.tv_usec / 100),
		 (int) rusage.ru_stime.tv_sec,
		 (int) (rusage.ru_stime.tv_usec / 100));

	va_start (args, format);
	vfprintf (checkpoint_file, format, args);
	va_end (args);

	fputs ("\n", checkpoint_file);
	fflush (checkpoint_file);
}