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
|
/*
* Copyright (c) Linux community.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include "auto-start.h"
#include "support.h"
#include "utility.h"
#include "dconf.h"
#include "log.h"
#define AUTO_START_PATH ".config/autostart/"
#define AUTO_START_FILENAME "audio-recorder.desktop"
static gchar *get_autostart_filename();
static gchar *get_desktop_filename();
static void create_autostart_directory();
static gchar *autostart_get_default_content();
void autostart_set(gboolean on_off) {
// Re-create $HOME/.config/autostart/audio-recorder.desktop file.
// Take a copy of /usr/share/applications/audio-recorder.desktop,
// and set the field "X-GNOME-Autostart-enabled" to "false" or "true".
// autostart_file, normally: $HOME/.config/autostart/audio-recorder.desktop
gchar *autostart_file = get_autostart_filename();
// desktop_file: normally /usr/share/applications/audio-recorder.desktop (shown in menus, toolbars and desktop surface)
gchar *desktop_file = get_desktop_filename();
// Ref: http://library.gnome.org/devel/glib/unstable/glib-Key-value-file-parser.html
GKeyFile *key_file = g_key_file_new();
GError *error = NULL;
g_key_file_load_from_file(key_file, desktop_file, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
if (error) {
LOG_ERROR("Cannot read file %s. %s\n", desktop_file, error->message);
g_error_free(error);
// Get default content
gchar *text = autostart_get_default_content();
error = NULL;
g_key_file_load_from_data(key_file, text, -1, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
g_free(text);
}
if (error) {
LOG_ERROR("Cannot read file %s. %s\n", desktop_file, error->message);
g_error_free(error);
// Cannot continue
goto LBL_1;
}
// Enable/disable auto start.
// Set the value of "X-GNOME-Autostart-enabled" to TRUE or FALSE.
g_key_file_set_boolean(key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Autostart-enabled", on_off);
// Create Exec line; "audio-recorder --show-window=0 --show-icon=1".
// It hides the main window and shows the systray icon.
gchar *cmd = g_strdup_printf("%s --show-window=0 --show-icon=1", PACKAGE);
g_key_file_set_string(key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, cmd);
g_free(cmd);
// Get key_file as text
gchar *text = g_key_file_to_data(key_file, NULL, NULL);
// Make sure $HOME/.config/autostart/ directory exists.
create_autostart_directory();
// Save to autostart_file
error = NULL;
save_file_content(autostart_file, text, &error);
if (error) {
LOG_ERROR("Cannot write to file %s. %s\n", autostart_file, error->message);
g_error_free(error);
}
// Free the text
g_free(text);
LBL_1:
// Free values
g_key_file_free(key_file);
g_free(autostart_file);
g_free(desktop_file);
}
gboolean autostart_get() {
// Get current auto-start value.
// Read $HOME/.config/autostart/audio-recorder.desktop file and return value of "X-GNOME-Autostart-enabled".
// auto_start_file, normally: $HOME/.config/autostart/audio-recorder.desktop
gchar *auto_start_file = get_autostart_filename();
gboolean ret = TRUE;
GKeyFile *key_file = g_key_file_new();
GError *error = NULL;
g_key_file_load_from_file(key_file, auto_start_file, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
if (error) {
g_error_free(error);
ret = FALSE;
goto LBL_1;
}
error = NULL;
ret = g_key_file_get_boolean(key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Autostart-enabled", &error);
if (error) {
g_error_free(error);
ret = FALSE;
}
LBL_1:
// Free values
g_key_file_free(key_file);
g_free(auto_start_file);
return ret;
}
static gchar *get_desktop_filename() {
// Return path+name for the .desktop file.
// Normally: /usr/share/applications/audio-recorder.desktop
gchar *data_dir = get_data_directory();
gchar *desktop_file = g_strdup_printf("%s/applications/%s", data_dir, AUTO_START_FILENAME);
g_free(data_dir);
// Caller should g_free() this value
return desktop_file;
}
static gchar *get_autostart_filename() {
// Return path+name for the auto start file.
// Get $HOME
gchar *home = get_home_dir();
gchar *filename = g_build_filename(home, AUTO_START_PATH, AUTO_START_FILENAME, NULL);
g_free(home);
// Caller should g_free() this value
return filename;
}
static void create_autostart_directory() {
// Create "$HOME/.config/autostart/" directory.
// It is normally created by "Autostart Applications" dialog, but it's initially missing in new GNOME-installations.
gchar *home = get_home_dir();
gchar *path = g_build_filename(home, AUTO_START_PATH, NULL);
// Create autostart directory
if (g_mkdir_with_parents(path, 0700) == -1) {
LOG_ERROR("Cannot create path \"%s\"\n", path);
}
g_free(home);
g_free(path);
}
static gchar *autostart_get_default_content() {
// Default audio-recorder.desktop content
return g_strdup("\n"
"[Desktop Entry]\n"
"GenericName=Audio Recorder\n"
"Type=Application\n"
"Exec=audio-recorder --show-window=0 --show-icon=1\n"
"Hidden=false\n"
"NoDisplay=false\n"
"Categories=GNOME;AudioVideo;Recorder\n"
"X-GNOME-Autostart-enabled=false\n"
"Name=Audio Recorder\n"
"Name[en_US]=Audio Recorder\n"
"Comment=Audio recorder application\n"
"Comment[en_US]=Easy-to-use audio recording tool\n");
}
|