summaryrefslogtreecommitdiff
path: root/src-ng/nautilus-main.c
blob: 57f5e8040c2184e61c6142288938cac1a32836b4 (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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <locale.h>
#include <stdlib.h>

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

#include "nautilus-directory.h"
#include "nautilus-file.h"
#include "nautilus-task.h"
#include "nautilus-tasks.h"

static void
got_info (NautilusFile *file,
          GFileInfo    *info,
          GError       *error,
          gpointer      user_data)
{
    g_print ("Got info for %p\n"
             "\tDisplay name: %s\n"
             "\tFile is directory: %s\n\n",
             (gpointer) file,
             g_file_info_get_display_name (info),
             NAUTILUS_IS_DIRECTORY (file)? "yes" : "no");

    g_object_unref (info);

    if (user_data != NULL)
    {
        g_main_loop_quit ((GMainLoop *) user_data);
    }
}

static void
got_children (NautilusDirectory *directory,
              GList             *children,
              GError            *error,
              gpointer           user_data)
{
    g_print ("Got children for %p\n", (gpointer) directory);

    if (children == NULL)
    {
        g_list_free (children);

        g_main_loop_quit ((GMainLoop *) user_data);

        return;
    }

    for (GList *i = children; i != NULL; i = i->next)
    {
        if (G_UNLIKELY (i->next == NULL))
        {
            nautilus_file_query_info (NAUTILUS_FILE (i->data),
                                      NULL, got_info, user_data);
        }
        else
        {
            nautilus_file_query_info (NAUTILUS_FILE (i->data),
                                      NULL, got_info, NULL);
        }
    }

    g_list_free (children);
}

static void
perform_self_test_checks (const gchar *path)
{
    g_autoptr (GFile) location = NULL;
    g_autoptr (NautilusFile) file = NULL;
    g_autoptr (NautilusFile) duplicate_file = NULL;
    GMainLoop *loop;

    location = g_file_new_for_path (path);

    g_print ("Creating NautilusFile\n");
    file = nautilus_file_new (location);
    g_print ("\tGot %p\n\n", (gpointer) file);

    g_print ("Creating another NautilusFile for the same location\n");
    duplicate_file = nautilus_file_new (location);
    g_print ("\tGot %p, which is %s\n\n",
             (gpointer) duplicate_file,
             file == duplicate_file? "the same" : "not the same");

    loop = g_main_loop_new (NULL, TRUE);

    if (NAUTILUS_IS_DIRECTORY (file))
    {
        nautilus_file_query_info (file, NULL, got_info, NULL);
    }
    else
    {
        nautilus_file_query_info (file, NULL, got_info, loop);
    }

    if (NAUTILUS_IS_DIRECTORY (file))
    {
        nautilus_directory_enumerate_children (NAUTILUS_DIRECTORY (file),
                                               NULL,
                                               got_children,
                                               loop);
    }

    g_main_loop_run (loop);
}

static void
on_children_changed (NautilusDirectory *directory,
                     gpointer           user_data)
{
    g_main_loop_quit ((GMainLoop *) user_data);
}

static void
_rename (const gchar *target,
         const gchar *name)
{
    g_autoptr (GFile) location = NULL;
    g_autoptr (NautilusFile) file = NULL;
    g_autoptr (NautilusFile) parent = NULL;
    g_autoptr (GHashTable) targets = NULL;
    g_autoptr (NautilusTask) task = NULL;
    GMainLoop *loop;

    location = g_file_new_for_path (target);
    g_message ("Constructed GFile %p for path %s",
               (gpointer) location, target);
    file = nautilus_file_new (location);
    parent = nautilus_file_get_parent (file);
    g_message ("Constructed NautilusFile %p for location %p",
               (gpointer) file, (gpointer) location);
    targets = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
    task = nautilus_task_new_with_func (nautilus_rename_task_func, targets, NULL);
    loop = g_main_loop_new (NULL, TRUE);

    (void) g_hash_table_insert (targets, location, name);

    g_signal_connect (parent, "children-changed",
                      G_CALLBACK (on_children_changed), loop);

    nautilus_task_run (task);

    g_main_loop_run (loop);
}

/*static void
on_thumbnail_finished (NautilusThumbnailTask *task,
                       GFile                 *location,
                       GdkPixbuf             *thumbnail,
                       gpointer               user_data)
{
    GtkWidget *image;

    image = gtk_image_new_from_pixbuf (thumbnail);

    gtk_widget_set_halign (image, GTK_ALIGN_CENTER);
    gtk_widget_set_valign (image, GTK_ALIGN_CENTER);
    gtk_widget_set_visible (image, TRUE);

    gtk_container_add (GTK_CONTAINER (user_data), image);

    g_object_unref (thumbnail);
}*/

static gboolean
on_window_deleted (GtkWidget *widget,
                   GdkEvent  *event,
                   gpointer   user_data)
{
    gtk_main_quit ();

    return GDK_EVENT_PROPAGATE;
}

static void
display_thumbnail (const gchar *path)
{
    /*GtkWidget *window;
    g_autoptr (GFile) location = NULL;
    g_autoptr (NautilusTask) task = NULL;
    g_autoptr (NautilusTaskManager) task_manager = NULL;

    gtk_init (NULL, NULL);

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    location = g_file_new_for_path (path);
    task = nautilus_thumbnail_task_new (location, TRUE);
    task_manager = nautilus_task_manager_dup_singleton ();

    gtk_widget_show_all (window);

    g_signal_connect_after (window, "delete-event", G_CALLBACK (on_window_deleted), NULL);
    g_signal_connect (task, "finished", G_CALLBACK (on_thumbnail_finished), window);

    nautilus_task_manager_queue_task (task_manager, task);

    gtk_main ();*/
}

int
main (int    argc,
      char **argv)
{
    g_autoptr (GOptionContext) option_context = NULL;
    gchar **files = NULL;
    gboolean check = FALSE;
    gchar *new_name = NULL;
    gboolean thumbnail = FALSE;
    const GOptionEntry option_entries[] =
    {
        {
            G_OPTION_REMAINING, 0, G_OPTION_FLAG_NONE,
            G_OPTION_ARG_FILENAME_ARRAY, &files, NULL, NULL
        },
        {
            "check", 'c', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &check,
            "Perform self-test checks with FILE as input", NULL
        },
        {
            "rename", 'n', G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, &new_name,
            "Rename FILE to NAME", "NAME"
        },
        {
            "thumbnail", 't', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &thumbnail,
            "Display thumbnail for FILE", NULL
        },
        { NULL }
    };
    GError *error = NULL;

    setlocale (LC_ALL, "");

    option_context = g_option_context_new ("[FILE]");

    g_option_context_add_main_entries (option_context, option_entries, NULL);

    if (!g_option_context_parse (option_context, &argc, &argv, &error))
    {
        g_print ("%s\n", error->message);

        return EXIT_FAILURE;
    }

    if (files == NULL)
    {
        g_print ("No input file specified\n");

        return EXIT_FAILURE;
    }

    if (check)
    {
        perform_self_test_checks (files[0]);
    }

    if (new_name != NULL && new_name[0] != '\0')
    {
        _rename (files[0], new_name);
    }

    if (thumbnail)
    {
        display_thumbnail (files[0]);
    }

    return EXIT_SUCCESS;
}