~audio-recorder/audio-recorder/trunk

22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
1
/*
1424 by Osmo Antero
Version 3.0.0
2
 * Copyright (c) 2011- Osmo Antero.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
3
 *
4
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Library General Public
6
 * License as published by the Free Software Foundation; either
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
7
 * version 3 of the License (GPL3), or any later version.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
463 by Osmo Antero
Updated README and INSTALL files.
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
12
 * See the GNU Library General Public License 3 for more details.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
13
 *
14
 * You should have received a copy of the GNU Library General Public
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
15
 * License 3 along with this program; if not, see /usr/share/common-licenses/GPL file
1012.1.5 by David Rabel
GPL boilerplate updated in source files.
16
 * or <http://www.gnu.org/licenses/>.
239 by Osmo Antero
Moving to GPL3 license. All src/*.c should now comply to GPL3.
17
*/
1 by Osmo Antero Maatta
Initial import 17.jan.2011
18
#include <stdlib.h>
19
#include "auto-start.h"
20
#include "support.h"
21
#include "utility.h"
69 by Osmo Antero
Ported this application to GTK/GDK 3. Using now GSettings (and dconf) instead of GConf2
22
#include "dconf.h"
1 by Osmo Antero Maatta
Initial import 17.jan.2011
23
#include "log.h"
427 by Osmo Antero
Find defult audio card/audio source (a .monitor device that we can listen to and record from. See audio-sources.c. Let's hope this hack! works in most cases).
24
#include <glib.h>
25
#include <glib/gstdio.h>
1 by Osmo Antero Maatta
Initial import 17.jan.2011
26
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
27
#define AUTO_START_PATH ".config/autostart/"
1 by Osmo Antero Maatta
Initial import 17.jan.2011
28
#define AUTO_START_FILENAME "audio-recorder.desktop"
29
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
30
static gchar *get_autostart_filename();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
31
static gchar *get_desktop_filename();
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
32
static void create_autostart_directory();
33
static gchar *autostart_get_default_content();
422 by Osmo Antero
Small fix in src/auto-start.c. Notice: audio-recorder v1.5 REQUIRES GStreamer 1.4! Check \gst-launch-1.0 --version\ before compilation.
34
static void autostart_remove_file(gchar *autostart_file);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
35
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
36
void autostart_set(gboolean _on) {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
37
    // Re-create $HOME/.config/autostart/audio-recorder.desktop file.
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
38
    // Take a copy of /usr/share/applications/audio-recorder.desktop.
39
    // if _on == TRUE: Set the field "X-GNOME-Autostart-enabled" to "true".
40
    // if _on == FALSE: Remove autostart file from the system.
463 by Osmo Antero
Updated README and INSTALL files.
41
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
42
    GKeyFile *key_file = NULL;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
43
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
44
    // autostart_file, normally: $HOME/.config/autostart/audio-recorder.desktop
45
    gchar *autostart_file = get_autostart_filename();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
46
47
    // desktop_file: normally /usr/share/applications/audio-recorder.desktop (shown in menus, toolbars and desktop surface)
48
    gchar *desktop_file = get_desktop_filename();
49
422 by Osmo Antero
Small fix in src/auto-start.c. Notice: audio-recorder v1.5 REQUIRES GStreamer 1.4! Check \gst-launch-1.0 --version\ before compilation.
50
    // Ref. bug #1312524. Some users have reported problems with setting the autostart option to OFF/NO.
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
51
    if (_on == FALSE) {
422 by Osmo Antero
Small fix in src/auto-start.c. Notice: audio-recorder v1.5 REQUIRES GStreamer 1.4! Check \gst-launch-1.0 --version\ before compilation.
52
        // Delete autostart file and disable it entirely.
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
53
        autostart_remove_file(autostart_file);
54
        goto LBL_1;
463 by Osmo Antero
Updated README and INSTALL files.
55
    }
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
56
57
    // Activate autostart (_on == TRUE).
58
1012.1.4 by David Rabel
Replaced http by https wherever it was reasonable. Also replaced some broken links and link that where only redirecting.
59
    // Ref: https://developer.gnome.org/glib/unstable/glib-Key-value-file-parser.html
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
60
    key_file = g_key_file_new();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
61
    GError *error = NULL;
62
    g_key_file_load_from_file(key_file, desktop_file, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
63
    if (error) {
64
        LOG_ERROR("Cannot read file %s. %s\n", desktop_file, error->message);
65
66
        g_error_free(error);
67
68
        // Get default content
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
69
        gchar *text = autostart_get_default_content();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
70
        error = NULL;
71
        g_key_file_load_from_data(key_file, text, -1, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
72
        g_free(text);
73
    }
74
75
    if (error) {
76
        LOG_ERROR("Cannot read file %s. %s\n", desktop_file, error->message);
77
        g_error_free(error);
78
        // Cannot continue
79
        goto LBL_1;
80
    }
81
82
    // Enable/disable auto start.
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
83
    // Set the value of "X-GNOME-Autostart-enabled" to TRUE.
84
    g_key_file_set_boolean(key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Autostart-enabled", _on);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
85
317 by Osmo Antero
Fixing bug #1159341. Systray icon popped ON after reboot or logout.
86
    // Show either trayicon or window. Not both.
87
    gboolean show_icon = FALSE;
88
    conf_get_boolean_value("show-systray-icon", &show_icon);
89
90
    // Create exec line
91
    gchar *cmd = NULL;
92
    if (show_icon) {
93
94
        // Hide window, show trayicon (icon is switched on in the settings)
95
        cmd = g_strdup_printf("%s --show-window=0", PACKAGE);
96
97
    } else {
98
99
        // Show window, hide trayicon (icon is switched off in the settings)
100
        cmd = g_strdup_printf("%s --show-window=1", PACKAGE);
101
    }
102
1 by Osmo Antero Maatta
Initial import 17.jan.2011
103
    g_key_file_set_string(key_file, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, cmd);
104
    g_free(cmd);
105
106
    // Get key_file as text
107
    gchar *text = g_key_file_to_data(key_file, NULL, NULL);
108
317 by Osmo Antero
Fixing bug #1159341. Systray icon popped ON after reboot or logout.
109
    // Make sure we have $HOME/.config/autostart/ directory
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
110
    create_autostart_directory();
111
112
    // Save to autostart_file
1 by Osmo Antero Maatta
Initial import 17.jan.2011
113
    error = NULL;
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
114
    save_file_content(autostart_file, text, &error);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
115
    if (error) {
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
116
        LOG_ERROR("Cannot write to file %s. %s\n", autostart_file, error->message);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
117
        g_error_free(error);
118
    }
119
120
    // Free the text
121
    g_free(text);
122
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
123
LBL_1:
1 by Osmo Antero Maatta
Initial import 17.jan.2011
124
    // Free values
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
125
    if (key_file) {
126
        g_key_file_free(key_file);
127
    }
128
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
129
    g_free(autostart_file);
1 by Osmo Antero Maatta
Initial import 17.jan.2011
130
    g_free(desktop_file);
131
}
132
422 by Osmo Antero
Small fix in src/auto-start.c. Notice: audio-recorder v1.5 REQUIRES GStreamer 1.4! Check \gst-launch-1.0 --version\ before compilation.
133
static void autostart_remove_file(gchar *autostart_file) {
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
134
    // Delete autostart file from the system
135
136
    // Safety test
137
    if (g_file_test(autostart_file, G_FILE_TEST_IS_REGULAR)) {
138
        LOG_DEBUG("Removing autostart file:%s\n", autostart_file);
463 by Osmo Antero
Updated README and INSTALL files.
139
418 by Osmo Antero
Version 1.5-4. Code cleanup. Please read ChangeLog file for actual changes in version 1.5.
140
        // Delete
141
        g_remove(autostart_file);
142
143
        if (g_file_test(autostart_file, G_FILE_TEST_IS_REGULAR)) {
144
            LOG_ERROR("Cannot delete autostart file:%s. Is it write protected?", autostart_file);
145
        }
146
    }
147
148
}
149
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
150
gboolean autostart_get() {
1 by Osmo Antero Maatta
Initial import 17.jan.2011
151
    // Get current auto-start value.
152
    // Read $HOME/.config/autostart/audio-recorder.desktop file and return value of "X-GNOME-Autostart-enabled".
153
154
    // auto_start_file, normally: $HOME/.config/autostart/audio-recorder.desktop
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
155
    gchar *auto_start_file = get_autostart_filename();
1 by Osmo Antero Maatta
Initial import 17.jan.2011
156
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
157
    gboolean ret = TRUE;
1 by Osmo Antero Maatta
Initial import 17.jan.2011
158
159
    GKeyFile *key_file = g_key_file_new();
160
    GError *error = NULL;
161
    g_key_file_load_from_file(key_file, auto_start_file, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
162
    if (error) {
163
        g_error_free(error);
164
        ret = FALSE;
165
        goto LBL_1;
166
    }
167
168
    error = NULL;
169
    ret = g_key_file_get_boolean(key_file, G_KEY_FILE_DESKTOP_GROUP, "X-GNOME-Autostart-enabled", &error);
170
171
    if (error) {
172
        g_error_free(error);
173
        ret = FALSE;
174
    }
175
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
176
LBL_1:
1 by Osmo Antero Maatta
Initial import 17.jan.2011
177
    // Free values
178
    g_key_file_free(key_file);
179
    g_free(auto_start_file);
180
181
    return ret;
182
}
183
184
static gchar *get_desktop_filename() {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
185
    // Return path+name for the .desktop file.
186
    // Normally: /usr/share/applications/audio-recorder.desktop
1 by Osmo Antero Maatta
Initial import 17.jan.2011
187
    gchar *data_dir = get_data_directory();
188
    gchar *desktop_file = g_strdup_printf("%s/applications/%s", data_dir, AUTO_START_FILENAME);
189
    g_free(data_dir);
190
191
    // Caller should g_free() this value
192
    return desktop_file;
193
}
194
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
195
static gchar *get_autostart_filename() {
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
196
    // Return path+name for the auto start file.
1 by Osmo Antero Maatta
Initial import 17.jan.2011
197
    // Get $HOME
198
    gchar *home = get_home_dir();
199
    gchar *filename = g_build_filename(home, AUTO_START_PATH, AUTO_START_FILENAME, NULL);
200
    g_free(home);
201
202
    // Caller should g_free() this value
203
    return filename;
204
}
205
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
206
static void create_autostart_directory() {
207
    // Create "$HOME/.config/autostart/" directory.
208
    // It is normally created by "Autostart Applications" dialog, but it's initially missing in new GNOME-installations.
209
    gchar *home = get_home_dir();
210
    gchar *path = g_build_filename(home, AUTO_START_PATH, NULL);
211
212
    // Create autostart directory
213
    if (g_mkdir_with_parents(path, 0700) == -1) {
214
        LOG_ERROR("Cannot create path \"%s\"\n", path);
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
215
    }
15 by Osmo Antero Maatta
Betterments after first test on Ubuntu 11.04 (Natty)
216
    g_free(home);
217
    g_free(path);
218
}
219
220
static gchar *autostart_get_default_content() {
221
    // Default audio-recorder.desktop content
1 by Osmo Antero Maatta
Initial import 17.jan.2011
222
223
    return g_strdup("\n"
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
224
                    "[Desktop Entry]\n"
47 by Osmo Antero Maatta
Formatted code (in src/*.[ch]) with astyle. Ref. README file.
225
                    "GenericName=Audio Recorder\n"
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
226
                    "Type=Application\n"
317 by Osmo Antero
Fixing bug #1159341. Systray icon popped ON after reboot or logout.
227
                    "Exec=audio-recorder --show-window=1\n"
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
228
                    "Hidden=false\n"
229
                    "NoDisplay=false\n"
35 by Osmo Antero Maatta
Mainly a small typo in auto-start.c
230
                    "Categories=GNOME;AudioVideo;Recorder\n"
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
231
                    "X-GNOME-Autostart-enabled=false\n"
232
                    "Name=Audio Recorder\n"
233
                    "Name[en_US]=Audio Recorder\n"
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
234
                    "Comment=Audio recorder application\n"
22 by Osmo Antero Maatta
Formatted the code using astyle. See README for instructions.
235
                    "Comment[en_US]=Easy-to-use audio recording tool\n");
1 by Osmo Antero Maatta
Initial import 17.jan.2011
236
}
237
31 by Osmo Antero Maatta
Timer commands are no longer translatable. Timer commands must be in English language.
238