File: tsh-tree-common.c

package info (click to toggle)
thunar-vcs-plugin 0.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 4,832 kB
  • sloc: ansic: 14,489; sh: 4,174; makefile: 306
file content (252 lines) | stat: -rw-r--r-- 6,295 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*-
 * Copyright (C) 2007-2011  Peter de Ridder <peter@xfce.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include <glib.h>
#include <gtk/gtk.h>

#include "tsh-tree-common.h"

static gint
path_compare (const gchar *path1, const gchar *path2)
{
  const gchar *i1, *i2;
  gchar c1, c2;
  gint depth = 0;
  i1 = path1;
  i2 = path2;
  while ((c1 = *i1++))
  {
    c2 = *i2++;

    if (c1 != c2)
    {
      if (c1 == '/'&& !c2)
        depth++;
      else if (!depth)
        depth = -1;

      return depth;
    }

    if (c1 == '/')
      depth++;
  }

  switch (*i2)
  {
    case '/':
      depth++;
      break;
    case '\0':
      return 0;
  }

  if (!depth)
    depth = -1;

  return depth;
}

static gint
path_depth (const gchar *path)
{
  const gchar *i;
  gchar c, last_c = '\0';
  gint depth = 0;
  i = path;
  while ((c = *i++))
  {
    if (c == '/')
      depth++;
    last_c = c;
  }
  if (last_c != '/')
    depth++;
  return depth;
}

static gchar*
path_get_prefix (const gchar *path, gint depth)
{
  const gchar *i;
  gchar c;
  gint lng = 0;
  i = path;
  while ((c = *i++))
  {
    if (c == '/')
      if (!--depth)
        break;
    lng++;
  }
  if (lng == 0)
    return g_strdup("/");
  return g_strndup (path, lng);
}

static const gchar*
path_remove_prefix (const gchar *path, gint depth)
{
  const gchar *i;
  gchar c;
  i = path;
  while ((c = *i++))
  {
    if (c == '/')
      if (!--depth)
        return i;
  }
  return NULL;
}

static void
move_children (GtkTreeStore *model, GtkTreeIter *dest, GtkTreeIter *src, gint path_column, TshTreeMoveInfoFunc move_info)
{
  gchar *path;
  GtkTreeIter iter_src;
  GtkTreeIter iter_dst;

  if (gtk_tree_model_iter_children (GTK_TREE_MODEL (model), &iter_src, src))
  {
    do
    {
      gtk_tree_store_append (model, &iter_dst, dest);
      gtk_tree_model_get (GTK_TREE_MODEL (model), &iter_src, path_column, &path, -1);
      move_info (model, &iter_dst, &iter_src);
      gtk_tree_store_set (model, &iter_dst, path_column, path, -1);
      g_free (path);

      move_children (model, &iter_dst, &iter_src, path_column, move_info);
    }
    while (gtk_tree_store_remove (model, &iter_src));
  }
}

void
tsh_tree_get_iter_for_path (GtkTreeStore *model, const gchar *file, GtkTreeIter *iter, gint path_column, TshTreeMoveInfoFunc move_info)
{
  const gchar *relative_file = file;
  GtkTreeIter real_parent, *parent = NULL;

  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), iter))
  {
    gint file_depth = path_depth (relative_file);
    for (;;)
    {
      gchar *path;
      gint depth;
      gtk_tree_model_get (GTK_TREE_MODEL (model), iter, path_column, &path, -1);

      depth = path_compare (path, relative_file);
      if(!depth)
      {
        g_free (path);
        return;
      }
      if (depth > 0)
      {
        gint depth_p = path_depth (path);

        if (depth_p == depth)
        {
          /* path is the parent */
          real_parent = *iter;
          parent = &real_parent;

          relative_file = path_remove_prefix (relative_file, depth);

          if (!gtk_tree_model_iter_children (GTK_TREE_MODEL (model), iter, parent))
          {
            g_free (path);
            break;
          }

          g_free(path);
          continue;
        }
        else if (depth == file_depth)
        {
          /* file is the parent */
          GtkTreeIter new;
          /* Add a new parent to the model, on iter removal iter will become this new iter */
          gtk_tree_store_insert_after (model, &real_parent, parent, iter);
          /* Add a child to copy the old iter's data to */
          gtk_tree_store_append (model, &new, &real_parent);
          /* Copy the data */
          move_info (model, &new, iter);
          /* Set the path names */
          gtk_tree_store_set (model, &real_parent, path_column, relative_file, -1);
          gtk_tree_store_set (model, &new, path_column, path_remove_prefix(path, depth), -1);
          /* Move the children */
          move_children (model, &new, iter, path_column, move_info);
          /* Iter becomes the next child, which is our new iter */
          gtk_tree_store_remove (model, iter);

          g_free (path);
          return;
        }
        else
        {
          /* common prefix */
          GtkTreeIter new;
          gchar *prefix = path_get_prefix (relative_file, depth);

          /* Add a new parent to the model */
          gtk_tree_store_insert_after (model, &real_parent, parent, iter);
          parent = &real_parent;
          /* Add a child to copy the old iter's data to */
          gtk_tree_store_append(model, &new, parent);
          /* Copy the data */
          move_info (model, &new, iter);
          /* Set the path names */
          gtk_tree_store_set (model, parent, path_column, prefix, -1);
          gtk_tree_store_set (model, &new, path_column, path_remove_prefix(path, depth), -1);
          /* Move the children */
          move_children (model, &new, iter, path_column, move_info);
          /* Remove the old iter */
          gtk_tree_store_remove (model, iter);

          relative_file = path_remove_prefix (relative_file, depth);

          g_free (prefix);
          g_free (path);
          break;
        }
      }

      g_free (path);
      if (!gtk_tree_model_iter_next (GTK_TREE_MODEL (model), iter))
        break;
    }
  }

  if (relative_file)
  {
    gtk_tree_store_append (model, iter, parent);
    gtk_tree_store_set (model, iter, path_column, relative_file, -1);
  }
}