summaryrefslogtreecommitdiff
path: root/src-ng/main.c
blob: df39bd4673b43220b4830b8ccf8838826995e54e (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
#include <locale.h>
#include <stdlib.h>

#include <glib.h>

#include "nautilus-directory.h"
#include "nautilus-file.h"
#include "nautilus-task-manager.h"
#include "tasks/nautilus-rename-task.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 (NautilusTaskManager) manager = 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);
    manager = nautilus_task_manager_dup_singleton ();
    task = nautilus_rename_task_new ();
    loop = g_main_loop_new (NULL, TRUE);

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

    nautilus_rename_task_add_target (NAUTILUS_RENAME_TASK (task),
                                     location, name);

    nautilus_task_manager_queue_task (manager, task);

    g_main_loop_run (loop);
}

int
main (int    argc,
      char **argv)
{
    g_autoptr (GOptionContext) option_context = NULL;
    gchar **files = NULL;
    gboolean check = FALSE;
    gchar *new_name = NULL;
    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"
        },
        { NULL }
    };
    GError *error = NULL;
    g_autoptr (NautilusTaskManager) manager = 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;
    }

    manager = nautilus_task_manager_dup_singleton ();

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

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

    return EXIT_SUCCESS;
}