Add example code to use beagle as a service provider.
[beagle.git] / search / Tiles / File.cs
blobafc9dd7e9438965dd1f04a359ee376319e9ad48d
1 using System;
2 using System.Diagnostics;
3 using System.Runtime.InteropServices;
4 using Mono.Unix;
5 using Beagle.Util;
7 namespace Search.Tiles {
9 public class FileActivator : TileActivator {
11 public FileActivator () : base ()
13 AddSupportedFlavor (new HitFlavor (null, "File", null));
16 public override Tile BuildTile (Beagle.Hit hit, Beagle.Query query)
18 return new TileFile (hit, query);
22 public class TileFile : TileTemplate {
24 public TileFile (Beagle.Hit hit, Beagle.Query query) : base (hit, query)
26 Title = GetTitle (hit);
27 EnableOpenWith = true;
29 if (Hit.FileInfo != null) {
30 Timestamp = Hit.FileInfo.LastWriteTimeUtc;
31 Description = Utils.NiceShortDate (Timestamp);
34 AddAction (new TileAction (Catalog.GetString ("Reveal in Folder"), RevealInFolder));
35 AddAction (new TileAction (Catalog.GetString ("E-Mail"), Email));
36 // AddAction (new TileAction (Catalog.GetString ("Instant-Message"), InstantMessage));
37 AddAction (new TileAction (Catalog.GetString ("Move to Trash"), Gtk.Stock.Delete, MoveToTrash));
40 static ThumbnailFactory thumbnailer = new ThumbnailFactory ();
42 protected override void LoadIcon (Gtk.Image image, int size)
44 // The File tile doesn't respect the icon size because
45 // 48 is too small for thumbnails
46 if (!thumbnailer.SetThumbnailIcon (image, Hit, size))
47 base.LoadIcon (image, size);
50 protected static string GetTitle (Beagle.Hit hit, bool get_parent)
52 string title;
54 if (get_parent)
55 title = Utils.GetFirstPropertyOfParent (hit, "dc:title");
56 else
57 title = hit.GetFirstProperty ("dc:title");
59 if (title == null || title == "") {
60 if (get_parent)
61 title = Utils.GetFirstPropertyOfParent (hit, "beagle:ExactFilename");
62 else
63 title = hit.GetFirstProperty ("beagle:ExactFilename");
66 return title;
69 protected static string GetTitle (Beagle.Hit hit)
71 return GetTitle (hit, false);
74 public override void Open ()
76 base.OpenFromMime (Hit);
79 public void OpenWith ()
81 // FIXME: base.OpenWith
84 public void RevealInFolder ()
86 string path = Hit.FileInfo.DirectoryName;
88 // FIXME: When nautilus implements this, then we should
89 // also select the file in the folder.
91 SafeProcess p = new SafeProcess ();
93 #if ENABLE_DESKTOP_LAUNCH
94 p.Arguments = new string [] { "desktop-launch", path };
95 #else
96 p.Arguments = new string [] { "nautilus", "--no-desktop", path };
97 #endif
98 try {
99 p.Start ();
100 } catch (Exception e) {
101 Console.WriteLine ("Cannot open folder: " + e);
105 public void Email ()
107 try {
108 OpenFromUri (String.Format ("mailto:?attach={0}", Hit.FileInfo.FullName));
109 } catch (Exception e) {
110 Console.WriteLine ("Error sending email: " + e);
114 public void InstantMessage ()
116 // FIXME: base.InstantMessage
119 public void MoveToTrash ()
121 // FIXME: Ask for confirmation
123 try {
124 // FIXME: Check if KDE uses ~/.Trash too (there is a spec at fd.o)
125 string trash_dir = System.IO.Path.Combine (Beagle.Util.PathFinder.HomeDir, ".Trash");
127 // FIXME: This throws an exception if the file exists
128 Hit.FileInfo.MoveTo (System.IO.Path.Combine (trash_dir, Hit.FileInfo.Name));
129 } catch (Exception e) {
130 Console.WriteLine (e);
134 protected override DetailsPane GetDetails ()
136 DetailsPane details = new DetailsPane ();
138 details.AddLabelPair (Catalog.GetString ("Title:"), GetTitle (Hit));
139 details.AddLabelPair (Catalog.GetString ("Last Edited:"), Utils.NiceLongDate (Timestamp));
141 if (Hit ["dc:author"] != null)
142 details.AddLabelPair (Catalog.GetString ("Author:"), Hit ["dc:author"]);
144 details.AddLabelPair (Catalog.GetString ("Full Path:"), Hit.Uri.LocalPath);
145 details.AddSnippet ();
147 return details;