~gero-bare/scratch/scratch-command-line-improvements

« back to all changes in this revision

Viewing changes to src/Scratch.vala

  • Committer: Geronimo Bareiro
  • Date: 2015-11-29 18:26:34 UTC
  • Revision ID: gero.bare@gmail.com-20151129182634-uzv2ipc0himdecss
Some rework done.

Improved error messages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
152
152
                foreach (string arg in args[1:unclaimed_args + 1]) {
153
153
                    // We set a message, that later is informed to the user
154
154
                    // in a dialog if something noteworthy happens.
155
 
                    string message = "";
 
155
                    string msg = "";
156
156
                    try {
157
157
                        var file = File.new_for_commandline_arg (arg);
158
158
 
159
 
                        if (!file.query_exists ())
160
 
                            FileUtils.set_contents (file.get_path (), "");
 
159
                        if (!file.query_exists ()) {
 
160
                            try {
 
161
                                FileUtils.set_contents (file.get_path (), "");
 
162
                            } catch (Error e) {
 
163
                                // We list some common errors for quick feedback
 
164
                                if (e is FileError.ACCES) {
 
165
                                    string reason = _("Maybe you do not have the necessary permissions.");
 
166
                                    msg = _("File \"%s\" cannot be created.\n%s").printf ("<b>%s</b>".printf (file.get_uri ()), reason);                                    
 
167
                                } else if (e is FileError.NOENT) {
 
168
                                    string reason = _("Maybe the file path provided is not valid.");
 
169
                                    msg = _("File \"%s\" cannot be created.\n%s").printf ("<b>%s</b>".printf (file.get_uri ()), reason);
 
170
                                } else if (e is FileError.ROFS) {
 
171
                                    string reason = _("The location is read-only.");
 
172
                                    msg = _("File \"%s\" cannot be created.\n%s").printf ("<b>%s</b>".printf (file.get_uri ()), reason);
 
173
                                } else {
 
174
                                    // Otherwise we simple use the error notification from glib
 
175
                                    msg = e.message;
 
176
                                }
 
177
                            }
 
178
                        }
161
179
 
162
180
                        var info = file.query_info ("standard::*", FileQueryInfoFlags.NONE, null);
163
 
                        if (info.get_file_type () == FileType.REGULAR)
 
181
                        if (info.get_file_type () == FileType.REGULAR
 
182
                            || info.get_file_type () == FileType.SYMBOLIC_LINK) {
164
183
                            files += file;
165
 
                        else
166
 
                            message = _("File \"%s\" cannot be opened.\nIt's not a regular file.").printf ("<b>%s</b>".printf (file.get_uri ()));
 
184
                        } else if (info.get_file_type () == FileType.MOUNTABLE){
 
185
                            string reason = _("Is a mountable location.");
 
186
                            msg = _("File \"%s\" cannot be opened.\n%s").printf ("<b>%s</b>".printf (file.get_uri ()), reason);
 
187
                        } else if (info.get_file_type () == FileType.DIRECTORY ){
 
188
                            string reason = _("Is a directory.");
 
189
                            msg = _("File \"%s\" cannot be opened.\n%s").printf ("<b>%s</b>".printf (file.get_uri ()), reason);
 
190
                        } else if (info.get_file_type () == FileType.SPECIAL ){
 
191
                            string reason = _("Is a \"special\" file such as a socket,\n fifo, block device, or character device.");
 
192
                            msg = _("File \"%s\" cannot be opened.\n%s").printf ("<b>%s</b>".printf (file.get_uri ()), reason);
 
193
                        } else {
 
194
                            string reason = _("Is a \"unknown\" file type.");
 
195
                            msg = _("File \"%s\" cannot be opened.\n%s").printf ("<b>%s</b>".printf (file.get_uri ()), reason);
 
196
                        }
167
197
 
168
198
                    } catch (Error e) {
169
 
                        // In case we failed create the file
170
 
                        if (e is FileError.PERM || e is FileError.ACCES) {
171
 
                            var file = File.new_for_commandline_arg (arg);
172
 
                            message = _("File \"%s\" cannot be created.\nMaybe you do not have the necessary permissions.").printf ("<b>%s</b>".printf (file.get_uri ()));
173
 
                        }
174
 
 
175
199
                        warning (e.message);
176
200
                    }
177
201
 
178
202
                    // Notify the user that something happened.
179
 
                    if (message.length > 0) {
 
203
                    if (msg.length > 0) {
180
204
                        var parent_window = get_last_window () as Gtk.Window;
181
205
                        var dialog = new Gtk.MessageDialog.with_markup (parent_window,
182
206
                            Gtk.DialogFlags.MODAL,
183
207
                            Gtk.MessageType.ERROR,
184
208
                            Gtk.ButtonsType.CLOSE,
185
 
                            message);
 
209
                            msg);
186
210
                        dialog.run ();
187
211
                        dialog.destroy ();
188
212
                        dialog.close ();