~ubuntu-branches/debian/stretch/gnome-builder/stretch

« back to all changes in this revision

Viewing changes to libide/ide-unsaved-file.c

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2015-10-11 12:38:45 UTC
  • Revision ID: package-import@ubuntu.com-20151011123845-a0hvkz01se0p1p5a
Tags: upstream-3.16.3
ImportĀ upstreamĀ versionĀ 3.16.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ide-unsaved-file.c
 
2
 *
 
3
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#define G_LOG_DOMAIN "ide-unsaved-file"
 
20
 
 
21
#include "ide-debug.h"
 
22
#include "ide-unsaved-file.h"
 
23
 
 
24
G_DEFINE_BOXED_TYPE (IdeUnsavedFile, ide_unsaved_file,
 
25
                     ide_unsaved_file_ref, ide_unsaved_file_unref)
 
26
 
 
27
struct _IdeUnsavedFile
 
28
{
 
29
  volatile gint  ref_count;
 
30
  GBytes        *content;
 
31
  GFile         *file;
 
32
  gchar         *temp_path;
 
33
  gint64         sequence;
 
34
};
 
35
 
 
36
IdeUnsavedFile *
 
37
_ide_unsaved_file_new (GFile       *file,
 
38
                       GBytes      *content,
 
39
                       const gchar *temp_path,
 
40
                       gint64       sequence)
 
41
{
 
42
  IdeUnsavedFile *ret;
 
43
 
 
44
  g_return_val_if_fail (G_IS_FILE (file), NULL);
 
45
  g_return_val_if_fail (content, NULL);
 
46
 
 
47
  ret = g_slice_new0 (IdeUnsavedFile);
 
48
  ret->ref_count = 1;
 
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);
 
53
 
 
54
  return ret;
 
55
}
 
56
 
 
57
const gchar *
 
58
ide_unsaved_file_get_temp_path (IdeUnsavedFile *self)
 
59
{
 
60
  g_return_val_if_fail (self, NULL);
 
61
 
 
62
  return self->temp_path;
 
63
}
 
64
 
 
65
gboolean
 
66
ide_unsaved_file_persist (IdeUnsavedFile  *self,
 
67
                          GCancellable    *cancellable,
 
68
                          GError         **error)
 
69
{
 
70
  gboolean ret;
 
71
 
 
72
  IDE_ENTRY;
 
73
 
 
74
  g_return_val_if_fail (self, FALSE);
 
75
  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);
 
76
 
 
77
  IDE_TRACE_MSG ("Saving draft to \"%s\"", self->temp_path);
 
78
 
 
79
  /*
 
80
   * TODO: Support cancellable.
 
81
   */
 
82
 
 
83
  ret = g_file_set_contents (self->temp_path,
 
84
                             g_bytes_get_data (self->content, NULL),
 
85
                             g_bytes_get_size (self->content),
 
86
                             error);
 
87
 
 
88
  IDE_RETURN (ret);
 
89
}
 
90
 
 
91
gint64
 
92
ide_unsaved_file_get_sequence (IdeUnsavedFile *self)
 
93
{
 
94
  g_return_val_if_fail (self, -1);
 
95
 
 
96
  return self->sequence;
 
97
}
 
98
 
 
99
IdeUnsavedFile *
 
100
ide_unsaved_file_ref (IdeUnsavedFile *self)
 
101
{
 
102
  g_return_val_if_fail (self, NULL);
 
103
  g_return_val_if_fail (self->ref_count > 0, NULL);
 
104
 
 
105
  g_atomic_int_inc (&self->ref_count);
 
106
 
 
107
  return self;
 
108
}
 
109
 
 
110
void
 
111
ide_unsaved_file_unref (IdeUnsavedFile *self)
 
112
{
 
113
  g_return_if_fail (self);
 
114
  g_return_if_fail (self->ref_count > 0);
 
115
 
 
116
  if (g_atomic_int_dec_and_test (&self->ref_count))
 
117
    {
 
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);
 
122
    }
 
123
}
 
124
 
 
125
/**
 
126
 * ide_unsaved_file_get_content:
 
127
 * @self: A #IdeUnsavedFile.
 
128
 *
 
129
 * Gets the contents of the unsaved file.
 
130
 *
 
131
 * Returns: (transfer none): A #GBytes containing the unsaved file content.
 
132
 */
 
133
GBytes *
 
134
ide_unsaved_file_get_content (IdeUnsavedFile *self)
 
135
{
 
136
  g_return_val_if_fail (self, NULL);
 
137
 
 
138
  return self->content;
 
139
}
 
140
 
 
141
/**
 
142
 * ide_unsaved_file_get_file:
 
143
 *
 
144
 * Retrieves the underlying file represented by @self.
 
145
 *
 
146
 * Returns: (transfer none): A #GFile.
 
147
 */
 
148
GFile *
 
149
ide_unsaved_file_get_file (IdeUnsavedFile *self)
 
150
{
 
151
  g_return_val_if_fail (self, NULL);
 
152
 
 
153
  return self->file;
 
154
}