~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/base/base.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <signal.h>
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
#ifdef HAVE_UNISTD_H
 
25
#include <unistd.h>
 
26
#endif
 
27
#include <sys/types.h>
 
28
 
 
29
#include <glib-object.h>
 
30
 
 
31
#ifdef G_OS_WIN32
 
32
#include <process.h>    /*  for _getpid()  */
 
33
#include <io.h>         /*  for _unlink()  */
 
34
#endif
 
35
 
 
36
#include "base-types.h"
 
37
 
 
38
#include "config/gimpbaseconfig.h"
 
39
#include "config/gimpconfig-path.h"
 
40
 
 
41
#include "paint-funcs/paint-funcs.h"
 
42
#include "composite/gimp-composite.h"
 
43
 
 
44
#include "base.h"
 
45
#include "temp-buf.h"
 
46
#include "tile-cache.h"
 
47
#include "tile-swap.h"
 
48
 
 
49
 
 
50
GimpBaseConfig *base_config = NULL;
 
51
 
 
52
 
 
53
static void   base_toast_old_temp_files   (GimpBaseConfig *config);
 
54
static void   base_tile_cache_size_notify (GObject        *config,
 
55
                                           GParamSpec     *param_spec,
 
56
                                           gpointer        data);
 
57
 
 
58
 
 
59
/*  public functions  */
 
60
 
 
61
gboolean
 
62
base_init (GimpBaseConfig *config,
 
63
           gboolean        be_verbose,
 
64
           gboolean        use_cpu_accel)
 
65
{
 
66
  gchar    *swapfile;
 
67
  gchar    *swapdir;
 
68
  gchar    *path;
 
69
  gboolean  swap_is_ok;
 
70
 
 
71
  g_return_val_if_fail (GIMP_IS_BASE_CONFIG (config), FALSE);
 
72
  g_return_val_if_fail (base_config == NULL, FALSE);
 
73
 
 
74
  base_config = g_object_ref (config);
 
75
 
 
76
  tile_cache_init (config->tile_cache_size);
 
77
 
 
78
  g_signal_connect (config, "notify::tile-cache-size",
 
79
                    G_CALLBACK (base_tile_cache_size_notify),
 
80
                    NULL);
 
81
 
 
82
  base_toast_old_temp_files (config);
 
83
 
 
84
  /* Add the swap file */
 
85
  if (! config->swap_path)
 
86
    g_object_set (config, "swap_path", "${gimp_dir}", NULL);
 
87
 
 
88
  swapdir  = gimp_config_path_expand (config->swap_path, TRUE, NULL);
 
89
  swapfile = g_strdup_printf ("gimpswap.%lu", (unsigned long) getpid ());
 
90
 
 
91
  path = g_build_filename (swapdir, swapfile, NULL);
 
92
 
 
93
  g_free (swapfile);
 
94
  g_free (swapdir);
 
95
 
 
96
  tile_swap_add (path, NULL, NULL);
 
97
 
 
98
  g_free (path);
 
99
 
 
100
  swap_is_ok = tile_swap_test ();
 
101
 
 
102
  gimp_composite_init (be_verbose, use_cpu_accel);
 
103
 
 
104
  paint_funcs_setup ();
 
105
 
 
106
  return swap_is_ok;
 
107
}
 
108
 
 
109
void
 
110
base_exit (void)
 
111
{
 
112
  g_return_if_fail (base_config != NULL);
 
113
 
 
114
  swapping_free ();
 
115
  paint_funcs_free ();
 
116
  tile_swap_exit ();
 
117
  tile_cache_exit ();
 
118
 
 
119
  g_signal_handlers_disconnect_by_func (base_config,
 
120
                                        base_tile_cache_size_notify,
 
121
                                        NULL);
 
122
 
 
123
  g_object_unref (base_config);
 
124
  base_config = NULL;
 
125
}
 
126
 
 
127
 
 
128
/*  private functions  */
 
129
 
 
130
static void
 
131
base_toast_old_temp_files (GimpBaseConfig *config)
 
132
{
 
133
  GDir       *dir = NULL;
 
134
  gchar      *dirname;
 
135
  const char *entry;
 
136
 
 
137
  if (!config->swap_path)
 
138
    return;
 
139
 
 
140
  dirname = gimp_config_path_expand (config->swap_path, TRUE, NULL);
 
141
  if (!dirname)
 
142
    return;
 
143
 
 
144
  dir = g_dir_open (dirname, 0, NULL);
 
145
 
 
146
  if (!dir)
 
147
    {
 
148
      g_free (dirname);
 
149
      return;
 
150
    }
 
151
 
 
152
  while ((entry = g_dir_read_name (dir)) != NULL)
 
153
    if (! strncmp (entry, "gimpswap.", 9))
 
154
      {
 
155
        /* don't try to kill swap files of running processes
 
156
         * yes, I know they might not all be gimp processes, and when you
 
157
         * unlink, it's refcounted, but lets not confuse the user by
 
158
         * "where did my disk space go?" cause the filename is gone
 
159
         * if the kill succeeds, and there running process isn't gimp
 
160
         * we'll probably get it the next time around
 
161
         */
 
162
 
 
163
        gint pid = atoi (entry + 9);
 
164
 
 
165
        /*  On Windows, you can't remove open files anyhow,
 
166
         *  so no harm trying.
 
167
         */
 
168
#ifndef G_OS_WIN32
 
169
        if (kill (pid, 0))
 
170
#endif
 
171
          {
 
172
            gchar *filename;
 
173
 
 
174
            filename = g_build_filename (dirname, entry, NULL);
 
175
            unlink (filename);
 
176
            g_free (filename);
 
177
          }
 
178
      }
 
179
 
 
180
  g_dir_close (dir);
 
181
 
 
182
  g_free (dirname);
 
183
}
 
184
 
 
185
static void
 
186
base_tile_cache_size_notify (GObject    *config,
 
187
                             GParamSpec *param_spec,
 
188
                             gpointer    data)
 
189
{
 
190
  tile_cache_set_size (GIMP_BASE_CONFIG (config)->tile_cache_size);
 
191
}