~ubuntu-branches/ubuntu/jaunty/muine/jaunty-proposed

« back to all changes in this revision

Viewing changes to src/CoverImage.cs

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2005-11-08 00:40:42 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051108004042-l6x5vw65u3f2l1dc
Tags: 0.8.3-7ubuntu2
Rebuild against new libdbus-1-cil

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2004 Jorn Baayen <jorn@nl.linux.org>
 
2
 * Copyright (C) 2004, 2005 Jorn Baayen <jbaayen@gnome.org>
3
3
 *
4
4
 * This program is free software; you can redistribute it and/or
5
5
 * modify it under the terms of the GNU General Public License as
21
21
using System.Text.RegularExpressions;
22
22
 
23
23
using Gtk;
 
24
using Gdk;
24
25
 
25
 
public class CoverImage : EventBox
 
26
namespace Muine
26
27
{
27
 
        private Image image;
28
 
        
29
 
        public CoverImage () : base ()
 
28
        public class CoverImage : EventBox
30
29
        {
31
 
                image = new Image ();   
32
 
                image.SetSizeRequest (CoverDatabase.AlbumCoverSize, 
33
 
                                      CoverDatabase.AlbumCoverSize);
 
30
                // Static
 
31
                // Static :: Variables
 
32
                // Static :: Variables :: Drag-and-Drop
 
33
                private static TargetEntry [] drag_entries = {
 
34
                        DndUtils.TargetUriList,
 
35
                        DndUtils.TargetGnomeIconList,
 
36
                        DndUtils.TargetNetscapeUrl
 
37
                };
 
38
 
 
39
                // Static :: Properties
 
40
                // Static :: Properties :: DragEntries
 
41
                public static TargetEntry [] DragEntries {
 
42
                        get { return drag_entries; }
 
43
                }
 
44
 
 
45
                // Static :: Methods
 
46
                // Static :: Methods :: HandleDrop
 
47
                //      TODO: Refactor
 
48
                /// <summary>
 
49
                ///     Handle a Drag-and-Drop event.
 
50
                /// </summary>
 
51
                /// <remarks>
 
52
                ///     This is called from <see cref="AddAlbumWindow" /> when
 
53
                ///     a cover is dropped there, and also from 
 
54
                ///     <see cref="OnDragDataReceived" />
 
55
                ///     when one is dropped somewhere else (e.g. in the
 
56
                ///     <see cref="PlaylistWindow" />).
 
57
                /// </remarks>
 
58
                /// <param name="song">
 
59
                ///     The <see cref="Song" /> which the cover is associated with.
 
60
                /// </param>
 
61
                /// <param name="args">
 
62
                ///     The <see cref="DragDataReceivedArgs" />.
 
63
                /// </param>
 
64
                public static void HandleDrop (Song song, DragDataReceivedArgs args)
 
65
                {
 
66
                        string data = DndUtils.SelectionDataToString (args.SelectionData);
 
67
 
 
68
                        bool success = false;
 
69
 
 
70
                        string [] uri_list;
 
71
                        string fn;
 
72
                        
 
73
                        switch (args.Info) {
 
74
                        case (uint) DndUtils.TargetType.Uri:
 
75
                                uri_list = Regex.Split (data, "\n");
 
76
                                fn = uri_list [0];
 
77
                                
 
78
                                Uri uri = new Uri (fn);
 
79
 
 
80
                                if (uri.Scheme != "http")
 
81
                                        break;
 
82
 
 
83
                                if (song.HasAlbum) {
 
84
                                        Album a = Global.DB.GetAlbum (song);
 
85
                                        a.SetCoverWeb (uri.AbsoluteUri);
 
86
 
 
87
                                } else {
 
88
                                        song.SetCoverWeb (uri.AbsoluteUri);
 
89
                                }
 
90
 
 
91
                                success = true;
 
92
 
 
93
                                break;
 
94
                                
 
95
                        case (uint) DndUtils.TargetType.UriList:
 
96
                                uri_list = DndUtils.SplitSelectionData (data);
 
97
                                fn = FileUtils.LocalPathFromUri (uri_list [0]);
 
98
 
 
99
                                if (fn == null)
 
100
                                        break;
 
101
 
 
102
                                try {
 
103
                                        if (song.HasAlbum) {
 
104
                                                Album a = Global.DB.GetAlbum (song);
 
105
                                                a.SetCoverLocal (fn);
 
106
 
 
107
                                        } else {
 
108
                                                song.SetCoverLocal (fn);
 
109
                                        }
 
110
                                                
 
111
                                        success = true;
 
112
 
 
113
                                } catch {
 
114
                                        success = false;
 
115
                                }
 
116
                                
 
117
                                break;
 
118
 
 
119
                        default:
 
120
                                break;
 
121
                        }
 
122
 
 
123
                        Gtk.Drag.Finish (args.Context, success, false, args.Time);
 
124
                }
 
125
 
 
126
                // Objects
 
127
                private Gtk.Image image;
 
128
                private Song song;
34
129
                
35
 
                Add (image);
36
 
 
37
 
                DragDataReceived += new DragDataReceivedHandler (HandleDragDataReceived);
38
 
 
39
 
                Muine.CoverDB.DoneLoading += new CoverDatabase.DoneLoadingHandler (HandleDoneLoading);
40
 
        }
41
 
 
42
 
        ~CoverImage ()
43
 
        {
44
 
                Dispose ();
45
 
        }
46
 
 
47
 
        private enum TargetType {
48
 
                UriList,
49
 
                Uri
50
 
        }
51
 
 
52
 
        private static TargetEntry [] cover_drag_entries = new TargetEntry [] {
53
 
                new TargetEntry ("text/uri-list", 0, (uint) TargetType.UriList),
54
 
                new TargetEntry ("x-special/gnome-icon-list", 0, (uint) TargetType.UriList),
55
 
                new TargetEntry ("_NETSCAPE_URL", 0, (uint) TargetType.Uri)
56
 
        };
57
 
 
58
 
        private void Sync ()
59
 
        {
60
 
                if (song != null && song.CoverImage != null)
61
 
                        image.FromPixbuf = song.CoverImage;
62
 
                else if (song != null && Muine.CoverDB.Loading)
63
 
                        image.FromPixbuf = Muine.CoverDB.DownloadingPixbuf;
64
 
                else {
65
 
                        image.SetFromStock ("muine-default-cover",
66
 
                                            StockIcons.AlbumCoverSize);
67
 
                }
68
 
        
69
 
                if (song != null && song.Album.Length > 0 && !Muine.CoverDB.Loading) {
70
 
                        Gtk.Drag.DestSet (this, DestDefaults.All,
71
 
                                          cover_drag_entries, Gdk.DragAction.Copy);
72
 
                } else {
73
 
                        Gtk.Drag.DestSet (this, DestDefaults.All,
74
 
                                          null, Gdk.DragAction.Copy);
75
 
                }
76
 
        }
77
 
 
78
 
        private Song song;
79
 
        public Song Song {
80
 
                set {
81
 
                        song = value;
82
 
 
 
130
                // Constructor
 
131
                /// <summary>
 
132
                ///     Create a new <see cref="CoverImage" />.
 
133
                /// </summary>
 
134
                public CoverImage () : base ()
 
135
                {
 
136
                        image = new Gtk.Image ();       
 
137
                        image.SetSizeRequest (CoverDatabase.CoverSize, 
 
138
                                              CoverDatabase.CoverSize);
 
139
                        
 
140
                        Add (image);
 
141
 
 
142
                        DragDataReceived += new DragDataReceivedHandler (OnDragDataReceived);
 
143
 
 
144
                        Global.CoverDB.DoneLoading += new CoverDatabase.DoneLoadingHandler (OnCoversDoneLoading);
 
145
                }
 
146
 
 
147
                // Destructor
 
148
                ~CoverImage ()
 
149
                {
 
150
                        Dispose ();
 
151
                }
 
152
 
 
153
                // Properties
 
154
                // Properties :: Song (set;)
 
155
                //      TODO: Add get?
 
156
                /// <summary>
 
157
                ///     The <see cref="Song" /> associated with this cover.
 
158
                /// </summary>
 
159
                /// <param name="value">
 
160
                ///     A <see cref="Song" />, or null to use a default cover.
 
161
                /// </param>
 
162
                public Song Song {
 
163
                        set {
 
164
                                song = value;
 
165
                                Sync ();
 
166
                        }
 
167
                }
 
168
 
 
169
                // Methods
 
170
                // Methods :: Private
 
171
                // Methods :: Private :: Sync
 
172
                /// <summary>
 
173
                ///     Synchronizes the image with the desired cover.
 
174
                /// </summary>
 
175
                /// <remarks>
 
176
                ///     If a cover is located in the database that is associated
 
177
                ///     with the same song that we are associated with, use that.
 
178
                ///     If we are associated with a song whose cover is currently
 
179
                ///     being downloaded, show a temporary cover. If we have no
 
180
                ///     associated song, use a default cover.
 
181
                /// </remarks>
 
182
                private void Sync ()
 
183
                {
 
184
                        // Image
 
185
                        if (song != null && song.CoverImage != null) {
 
186
                                image.FromPixbuf = song.CoverImage;
 
187
 
 
188
                        } else if (song != null && Global.CoverDB.Loading) {
 
189
                                image.FromPixbuf = Global.CoverDB.DownloadingPixbuf;
 
190
 
 
191
                        } else {
 
192
                                image.SetFromStock ("muine-default-cover", StockIcons.CoverSize);
 
193
                        }
 
194
 
 
195
                        // DnD Entries
 
196
                        TargetEntry [] entries;
 
197
                        
 
198
                        if (song != null && !Global.CoverDB.Loading)
 
199
                                entries = drag_entries;
 
200
                        else
 
201
                                entries = null;
 
202
 
 
203
                        // DnD Destination
 
204
                        Gtk.Drag.DestSet (this, DestDefaults.All, entries, Gdk.DragAction.Copy);
 
205
                }
 
206
 
 
207
                // Handlers
 
208
                // Handlers :: OnDragDataReceived
 
209
                /// <summary>
 
210
                ///     Handler called when Drag-and-Drop data is received.
 
211
                /// </summary>
 
212
                /// <remarks>
 
213
                ///     This just calls <see cref="HandleDrop" />.
 
214
                /// </remarks>
 
215
                /// <param name="o">
 
216
                ///     The calling object.
 
217
                /// </param>
 
218
                /// <param name="args">
 
219
                ///     The <see cref="DragDataReceivedArgs" />.
 
220
                /// </param>
 
221
                private void OnDragDataReceived (object o, DragDataReceivedArgs args)
 
222
                {
 
223
                        HandleDrop (song, args);
 
224
                }
 
225
 
 
226
                // Handlers :: OnCoversDoneLoading
 
227
                /// <summary>
 
228
                ///     Handler called when covers are done loading.
 
229
                /// </summary>
 
230
                /// <remarks>
 
231
                ///     This just calls <see cref="Sync" />.
 
232
                /// </remarks>
 
233
                private void OnCoversDoneLoading ()
 
234
                {
83
235
                        Sync ();
84
236
                }
85
237
        }
86
 
 
87
 
        public static void HandleDrop (Song song, DragDataReceivedArgs args)
88
 
        {
89
 
                string data = StringUtils.SelectionDataToString (args.SelectionData);
90
 
 
91
 
                bool success = false;
92
 
 
93
 
                string [] uri_list;
94
 
                string fn;
95
 
                
96
 
                switch (args.Info) {
97
 
                case (uint) TargetType.Uri:
98
 
                        uri_list = Regex.Split (data, "\n");
99
 
                        fn = uri_list [0];
100
 
                        
101
 
                        Uri uri = new Uri (fn);
102
 
 
103
 
                        if (!(uri.Scheme == "http"))
104
 
                                break;
105
 
 
106
 
                        success = true;
107
 
 
108
 
                        try {
109
 
                                if (Muine.CoverDB.Covers.ContainsKey (song.AlbumKey))
110
 
                                        Muine.CoverDB.RemoveCover (song.AlbumKey);
111
 
                                song.CoverImage = Muine.CoverDB.AddCoverDownloading (song.AlbumKey);
112
 
                                Muine.DB.SyncAlbumCoverImageWithSong (song);
113
 
                                
114
 
                                song.DownloadNewCoverImage (uri.AbsoluteUri);
115
 
 
116
 
                                success = true;
117
 
                        } catch (Exception e) {
118
 
                                success = false;
119
 
                                
120
 
                                break;
121
 
                        }
122
 
 
123
 
                        break;
124
 
                case (uint) TargetType.UriList:
125
 
                        uri_list = Regex.Split (data, "\r\n");
126
 
                        fn = StringUtils.LocalPathFromUri (uri_list [0]);
127
 
 
128
 
                        if (fn == null)
129
 
                                break;
130
 
 
131
 
                        try {
132
 
                                if (Muine.CoverDB.Covers.ContainsKey (song.AlbumKey))
133
 
                                        Muine.CoverDB.RemoveCover (song.AlbumKey);
134
 
                                song.CoverImage = Muine.CoverDB.AddCoverLocal (song.AlbumKey, fn);
135
 
                                Muine.DB.SyncAlbumCoverImageWithSong (song);
136
 
 
137
 
                                success = true;
138
 
                        } catch {
139
 
                                success = false;
140
 
                                
141
 
                                break;
142
 
                        }
143
 
                        
144
 
                        break;
145
 
                default:
146
 
                        break;
147
 
                }
148
 
 
149
 
                Drag.Finish (args.Context, success, false, args.Time);
150
 
        }
151
 
 
152
 
        private void HandleDragDataReceived (object o, DragDataReceivedArgs args)
153
 
        {
154
 
                HandleDrop (song, args);
155
 
        }
156
 
 
157
 
        private void HandleDoneLoading ()
158
 
        {
159
 
                Sync ();
160
 
        }
161
238
}