3
* Copyright (C) 2015 Christian Hergert <christian@hergert.me>
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#define G_LOG_DOMAIN "ide-unsaved-file"
21
#include "ide-debug.h"
22
#include "ide-unsaved-file.h"
24
G_DEFINE_BOXED_TYPE (IdeUnsavedFile, ide_unsaved_file,
25
ide_unsaved_file_ref, ide_unsaved_file_unref)
27
struct _IdeUnsavedFile
29
volatile gint ref_count;
37
_ide_unsaved_file_new (GFile *file,
39
const gchar *temp_path,
44
g_return_val_if_fail (G_IS_FILE (file), NULL);
45
g_return_val_if_fail (content, NULL);
47
ret = g_slice_new0 (IdeUnsavedFile);
49
ret->file = g_object_ref (file);
50
ret->content = g_bytes_ref (content);
51
ret->sequence = sequence;
52
ret->temp_path = g_strdup (temp_path);
58
ide_unsaved_file_get_temp_path (IdeUnsavedFile *self)
60
g_return_val_if_fail (self, NULL);
62
return self->temp_path;
66
ide_unsaved_file_persist (IdeUnsavedFile *self,
67
GCancellable *cancellable,
74
g_return_val_if_fail (self, FALSE);
75
g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
77
IDE_TRACE_MSG ("Saving draft to \"%s\"", self->temp_path);
80
* TODO: Support cancellable.
83
ret = g_file_set_contents (self->temp_path,
84
g_bytes_get_data (self->content, NULL),
85
g_bytes_get_size (self->content),
92
ide_unsaved_file_get_sequence (IdeUnsavedFile *self)
94
g_return_val_if_fail (self, -1);
96
return self->sequence;
100
ide_unsaved_file_ref (IdeUnsavedFile *self)
102
g_return_val_if_fail (self, NULL);
103
g_return_val_if_fail (self->ref_count > 0, NULL);
105
g_atomic_int_inc (&self->ref_count);
111
ide_unsaved_file_unref (IdeUnsavedFile *self)
113
g_return_if_fail (self);
114
g_return_if_fail (self->ref_count > 0);
116
if (g_atomic_int_dec_and_test (&self->ref_count))
118
g_clear_pointer (&self->temp_path, g_free);
119
g_clear_pointer (&self->content, g_bytes_unref);
120
g_clear_object (&self->file);
121
g_slice_free (IdeUnsavedFile, self);
126
* ide_unsaved_file_get_content:
127
* @self: A #IdeUnsavedFile.
129
* Gets the contents of the unsaved file.
131
* Returns: (transfer none): A #GBytes containing the unsaved file content.
134
ide_unsaved_file_get_content (IdeUnsavedFile *self)
136
g_return_val_if_fail (self, NULL);
138
return self->content;
142
* ide_unsaved_file_get_file:
144
* Retrieves the underlying file represented by @self.
146
* Returns: (transfer none): A #GFile.
149
ide_unsaved_file_get_file (IdeUnsavedFile *self)
151
g_return_val_if_fail (self, NULL);