~docky-core/plank/trunk

« back to all changes in this revision

Viewing changes to docklets/Clippy/ClippyDockItem.vala

  • Committer: Rico Tzschichholz
  • Date: 2015-11-02 08:40:11 UTC
  • Revision ID: ricotz@ubuntu.com-20151102084011-faalf09598xzh4ct
Add docklets support (internal/private)

Based on initially proposed docky 3.0 branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
//  Copyright (C) 2010-2011 Robert Dyer
 
3
//
 
4
//  This file is part of Plank.
 
5
//
 
6
//  Plank is free software: you can redistribute it and/or modify
 
7
//  it under the terms of the GNU General Public License as published by
 
8
//  the Free Software Foundation, either version 3 of the License, or
 
9
//  (at your option) any later version.
 
10
//
 
11
//  Plank is distributed in the hope that it will be useful,
 
12
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
//  GNU General Public License for more details.
 
15
//
 
16
//  You should have received a copy of the GNU General Public License
 
17
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
//
 
19
 
 
20
using Plank.Items;
 
21
 
 
22
namespace Docky
 
23
{
 
24
        public class ClippyDockItem : DockletItem
 
25
        {
 
26
                Gtk.Clipboard clipboard;
 
27
                Gee.ArrayList<string> clips = new Gee.ArrayList<string> ();
 
28
                int cur_position = 0;
 
29
                uint timer_id = 0U;
 
30
                
 
31
                /**
 
32
                 * {@inheritDoc}
 
33
                 */
 
34
                public ClippyDockItem.with_dockitem_file (GLib.File file)
 
35
                {
 
36
                        GLib.Object (Prefs: new ClippyPreferences.with_file (file));
 
37
                }
 
38
                
 
39
                construct
 
40
                {
 
41
                        unowned ClippyPreferences prefs = (ClippyPreferences) Prefs;
 
42
                        
 
43
                        Icon = "edit-cut";
 
44
                        
 
45
                        if (prefs.TrackMouseSelections)
 
46
                                clipboard = Gtk.Clipboard.get (Gdk.Atom.intern ("PRIMARY", true));
 
47
                        else
 
48
                                clipboard = Gtk.Clipboard.get (Gdk.Atom.intern ("CLIPBOARD", true));
 
49
                        
 
50
                        timer_id = Gdk.threads_add_timeout (prefs.TimerDelay, check_clipboard);
 
51
                        
 
52
                        updated ();
 
53
                }
 
54
                
 
55
                ~ClippyDockItem ()
 
56
                {
 
57
                        if (timer_id > 0U)
 
58
                                GLib.Source.remove (timer_id);
 
59
                }
 
60
                
 
61
                bool check_clipboard ()
 
62
                {
 
63
                        clipboard.request_text ((Gtk.ClipboardTextReceivedFunc) clipboard_text_received);
 
64
                        
 
65
                        return true;
 
66
                }
 
67
                
 
68
                [CCode (instance_pos = -1)]
 
69
                void clipboard_text_received (Gtk.Clipboard clipboard, string text)
 
70
                {
 
71
                        if (text == null || text == "")
 
72
                                return;
 
73
                        
 
74
                        unowned ClippyPreferences prefs = (ClippyPreferences) Prefs;
 
75
                        
 
76
                        clips.remove (text);
 
77
                        clips.add (text);
 
78
                        while (clips.size > prefs.MaxEntries)
 
79
                                clips.remove_at (0);
 
80
                        
 
81
                        cur_position = clips.size;
 
82
                        
 
83
                        updated ();
 
84
                }
 
85
                
 
86
                void updated ()
 
87
                {
 
88
                        if (clips.size == 0)
 
89
                                Text = _("Clipboard is currently empty.");
 
90
                        else if (cur_position == 0 || cur_position > clips.size)
 
91
                                Text = get_entry_at (clips.size);
 
92
                        else
 
93
                                Text = get_entry_at (cur_position);
 
94
                }
 
95
                
 
96
                string get_entry_at (int pos)
 
97
                {
 
98
                        return clips.get (pos - 1).replace ("\n", "").replace ("\t", "");
 
99
                }
 
100
                
 
101
                void copy_entry_at (int pos)
 
102
                {
 
103
                        if (pos < 1 || pos > clips.size)
 
104
                                return;
 
105
                        
 
106
                        var str = clips.get (pos - 1);
 
107
                        clipboard.set_text (str, (int) str.length);
 
108
                        
 
109
                        updated ();
 
110
                }
 
111
                
 
112
                void copy_entry ()
 
113
                {
 
114
                        if (cur_position == 0)
 
115
                                copy_entry_at (clips.size);
 
116
                        else
 
117
                                copy_entry_at (cur_position);
 
118
                }
 
119
                
 
120
                void clear ()
 
121
                {
 
122
                        clipboard.clear ();
 
123
                        clips.clear ();
 
124
                        cur_position = 0;
 
125
                        
 
126
                        updated ();
 
127
                }
 
128
                
 
129
                protected override Animation on_scrolled (Gdk.ScrollDirection direction, Gdk.ModifierType mod, uint32 event_time)
 
130
                {
 
131
                        if (direction == Gdk.ScrollDirection.UP)
 
132
                                cur_position++;
 
133
                        else
 
134
                                cur_position--;
 
135
                        
 
136
                        if (cur_position < 1)
 
137
                                cur_position = clips.size;
 
138
                        else if (cur_position > clips.size)
 
139
                                cur_position = 1;
 
140
                        
 
141
                        updated ();
 
142
                        
 
143
                        return Animation.NONE;
 
144
                }
 
145
                
 
146
                protected override Animation on_clicked (PopupButton button, Gdk.ModifierType mod, uint32 event_time)
 
147
                {
 
148
                        if (button == PopupButton.LEFT && clips.size > 0) {
 
149
                                copy_entry ();
 
150
                                return Animation.BOUNCE;
 
151
                        }
 
152
                        
 
153
                        return Animation.NONE;
 
154
                }
 
155
                
 
156
                public override Gee.ArrayList<Gtk.MenuItem> get_menu_items ()
 
157
                {
 
158
                        var items = new Gee.ArrayList<Gtk.MenuItem> ();
 
159
                        
 
160
                        for (var i = clips.size ; i > 0; i--) {
 
161
                                var item = create_menu_item (clips.get (i - 1), "edit-cut");
 
162
                                item.activate.connect (() => {
 
163
                                        copy_entry_at (i);
 
164
                                });
 
165
                                items.add (item);
 
166
                        }
 
167
                        
 
168
                        if (clips.size > 0) {
 
169
                                var item = create_menu_item (_("_Clear"), "edit-clear-all", true);
 
170
                                item.activate.connect (clear);
 
171
                                items.add (item);
 
172
                        }
 
173
                        
 
174
                        return items;
 
175
                }
 
176
        }
 
177
}