View | Details | Raw Unified | Return to bug 27258
Collapse All | Expand All

(-)a/extensions/external-applications.vala (+109 lines)
Line 0 Link Here
1
/*
2
 Copyright (C) 2010 Christian Dywan <christian@twotoasts.de>
3
4
 This library is free software; you can redistribute it and/or
5
 modify it under the terms of the GNU Lesser General Public
6
 License as published by the Free Software Foundation; either
7
 version 2.1 of the License, or (at your option) any later version.
8
9
 See the file COPYING for the full license text.
10
*/
11
12
using Gtk;
13
using WebKit;
14
using Midori;
15
16
public class ExternalApplications : Midori.Extension {
17
    Dialog? dialog;
18
    bool launch (string command, string uri) {
19
        try {
20
            var info = GLib.AppInfo.create_from_commandline (command + " " + uri, null, 0);
21
	    /*
22
            var uris = new List<string>();
23
            uris.prepend (uri);
24
            info.launch_uris (uris, null);
25
	    */
26
	    info.launch(null,null);
27
            return true;
28
        }
29
        catch (GLib.Error error) {
30
            var error_dialog = new Gtk.MessageDialog (null, 0,
31
                Gtk.MessageType.ERROR, Gtk.ButtonsType.OK,
32
                "Failed to launch external application.");
33
            error_dialog.format_secondary_text (error.message);
34
            error_dialog.response.connect ((dialog, response)
35
                => { dialog.destroy (); });
36
            error_dialog.show ();
37
        }
38
        return false;
39
    }
40
    bool navigating (WebFrame web_frame, NetworkRequest request,
41
                     WebNavigationAction action, WebPolicyDecision decision) {
42
        string uri = request.get_uri ();
43
        if (uri.has_prefix ("ftp://")) {
44
            if (launch ("pcmanfm", uri)) {
45
                decision.ignore ();
46
                return true;
47
            }
48
        }
49
        return false;
50
    }
51
    void tab_added (View tab) {
52
        var web_view = tab.get_web_view ();
53
        web_view.navigation_policy_decision_requested.connect (navigating);
54
    }
55
    void configure_external_applications () {
56
        if (dialog == null) {
57
            dialog = new Dialog.with_buttons ("Configure External Applications",
58
                get_app ().browser,
59
                DialogFlags.DESTROY_WITH_PARENT | DialogFlags.NO_SEPARATOR,
60
                STOCK_CLOSE, ResponseType.CLOSE);
61
            dialog.icon_name = STOCK_PROPERTIES;
62
            dialog.destroy.connect ((dialog) => { dialog = null; });
63
            dialog.response.connect ((dialog, response) => { dialog.destroy (); });
64
            dialog.show ();
65
        }
66
        else
67
            dialog.present ();
68
    }
69
    void tool_menu_populated (Gtk.Menu menu) {
70
        var menuitem = new Gtk.MenuItem.with_mnemonic ("Configure _External Applications...");
71
        menuitem.activate.connect (configure_external_applications);
72
        menuitem.show ();
73
        menu.append (menuitem);
74
    }
75
    void browser_added (Browser browser) {
76
        foreach (var tab in browser.get_tabs ())
77
            tab_added (tab);
78
        browser.add_tab.connect (tab_added);
79
        browser.populate_tool_menu.connect (tool_menu_populated);
80
    }
81
    void activated (Midori.App app) {
82
        foreach (var browser in app.get_browsers ())
83
            browser_added (browser);
84
        app.add_browser.connect (browser_added);
85
    }
86
    void deactivated () {
87
        var app = get_app ();
88
        app.add_browser.disconnect (browser_added);
89
        foreach (var browser in app.get_browsers ()) {
90
            foreach (var tab in browser.get_tabs ())
91
                /* */;
92
            browser.populate_tool_menu.disconnect (tool_menu_populated);
93
        }
94
    }
95
    internal ExternalApplications () {
96
        activate.connect (activated);
97
        deactivate.connect (deactivated);
98
    }
99
}
100
101
public Midori.Extension extension_init () {
102
    var extension = new ExternalApplications ();
103
    extension.name = "External Applications";
104
    extension.description = "Pass ftp:// URLs to pcmanfm";
105
    extension.version = "0.1";
106
    extension.authors = "Christian Dywan <christian@twotoasts.de>";
107
    return extension;
108
}
109

Return to bug 27258