summaryrefslogtreecommitdiff
path: root/libnautilus-extensions/nautilus-medusa-support.c
blob: a4e1dc2a579a69885cc04260efe2d7c0aa03c692 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/* nautilus-medusa-support.c - Covers for access to medusa
   from nautilus

   Copyright (C) 2001 Eazel, Inc.

   The Gnome 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.

   The Gnome 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 the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Authors: Ramiro Estrugo <ramiro@eazel.com>
            Rebecca Schulman <rebecka@eazel.com>
*/

#include <config.h>
#include "nautilus-medusa-support.h"

#include <eel/eel-glib-extensions.h>
#include <eel/eel-string.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/types.h>

#ifdef HAVE_MEDUSA
#include <libmedusa/medusa-system-state.h>
#endif

gboolean
nautilus_medusa_services_have_been_enabled_by_user (void)
{
#ifdef HAVE_MEDUSA
	return medusa_system_services_have_been_enabled_by_user (g_get_user_name ());
#else
	return FALSE;
#endif
}

gboolean
nautilus_medusa_blocked (void)
{
#ifdef HAVE_MEDUSA
	return medusa_system_services_are_blocked ();
#else
	return TRUE;
#endif
}

void
nautilus_medusa_enable_services (gboolean enable)
{
#ifdef HAVE_MEDUSA
	medusa_enable_medusa_services (enable);
#endif
}

void
nautilus_medusa_add_system_state_changed_callback (NautilusMedusaChangedCallback callback,
						   gpointer callback_data)
{
#ifdef HAVE_MEDUSA
	medusa_execute_when_system_state_changes (MEDUSA_SYSTEM_STATE_ENABLED
						  | MEDUSA_SYSTEM_STATE_DISABLED
						  | MEDUSA_SYSTEM_STATE_BLOCKED,
						  callback,
						  callback_data);
#endif
}


NautilusCronStatus
nautilus_medusa_check_cron_is_enabled (void)
{
	DIR *proc_directory;
	struct dirent *file;
	char *stat_file_name;
	FILE *stat_file;
	char stat_file_data[128];
	const char *stat_file_process_name;
	int process_number, bytes_read;
	NautilusCronStatus status;

	/* We figure out whether cron is running by reading the proc
	   directory, and checking for a process named or ending with
	   "crond" */

	proc_directory = opendir ("/proc");
	if (proc_directory == NULL) {
		return NAUTILUS_CRON_STATUS_UNKNOWN;
	}

	status = NAUTILUS_CRON_STATUS_UNKNOWN;

	while ((file = readdir (proc_directory)) != NULL) {
		/* Process files have numbers */
		if (!eel_str_to_int (file->d_name, &process_number)) {
			continue;
		}

		/* Since we've seen at least one process file, we can change our state
		 * from "unknown" to "presumed off until proved otherwise".
		 */
		status = NAUTILUS_CRON_STATUS_OFF;

		stat_file_name = g_strdup_printf ("/proc/%d/stat", process_number);
		stat_file = fopen (stat_file_name, "r");
		g_free (stat_file_name);
		
		if (stat_file == NULL) {
			continue;
		}
		
		bytes_read = fread (stat_file_data, 1, sizeof (stat_file_data) - 1, stat_file);
		fclose (stat_file);
		stat_file_data[bytes_read] = '\0';
		
		stat_file_process_name = strchr (stat_file_data, ' ');
		
		if (eel_str_has_prefix (stat_file_process_name, " (crond)") ||
		    eel_str_has_prefix (stat_file_process_name, " (cron)")) {
			status = NAUTILUS_CRON_STATUS_ON;
			break;
		}
	}

	closedir (proc_directory);
	return status;
}