PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/giver-0.1.8/src/TargetService.cs

#
C# | 376 lines | 280 code | 56 blank | 40 comment | 25 complexity | f056b7c3b78d5e8c15b45ebf240065a8 MD5 | raw file
  1. /***************************************************************************
  2. * TargetService.cs
  3. *
  4. * Copyright (C) 2007 Novell, Inc.
  5. * Written by Calvin Gaisford <calvinrg@gmail.com>
  6. ****************************************************************************/
  7. /* THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Collections;
  30. using System.IO;
  31. using Gtk;
  32. using Mono.Unix;
  33. namespace Giver
  34. {
  35. ///<summary>
  36. /// TargetWindow
  37. /// Window holding all drop targets for giver
  38. ///</summary>
  39. public enum DragTargetType
  40. {
  41. UriList,
  42. TomBoyNote
  43. };
  44. /// <summary>
  45. /// Widget used to show Avahi Services for Giver
  46. /// </summary>
  47. public class TargetService : Gtk.Button
  48. {
  49. private ServiceInfo serviceInfo;
  50. private bool isManual;
  51. private Gtk.Image image;
  52. private double progressFraction;
  53. private bool updateProgress;
  54. private ProgressBar progressBar;
  55. private Label progressLabel;
  56. public TargetService(ServiceInfo serviceInfo)
  57. {
  58. this.serviceInfo = serviceInfo;
  59. isManual = false;
  60. Init();
  61. }
  62. public TargetService()
  63. {
  64. isManual = true;
  65. Init();
  66. }
  67. private void Init()
  68. {
  69. this.BorderWidth = 0;
  70. this.Relief = Gtk.ReliefStyle.None;
  71. this.CanFocus = false;
  72. progressFraction = 0;
  73. updateProgress = false;
  74. VBox outerVBox = new VBox (false, 4);
  75. HBox hbox = new HBox(false, 10);
  76. if(isManual) {
  77. image = new Gtk.Image(Utilities.GetIcon("computer", 48));
  78. } else {
  79. if(serviceInfo.Photo != null)
  80. image = new Gtk.Image(serviceInfo.Photo);
  81. else
  82. image = new Gtk.Image(Utilities.GetIcon("giver-48", 48));
  83. }
  84. hbox.PackStart(image, false, false, 0);
  85. VBox vbox = new VBox();
  86. hbox.PackStart(vbox, false, false, 0);
  87. Label label = new Label();
  88. label.Justify = Gtk.Justification.Left;
  89. label.SetAlignment (0.0f, 0.5f);
  90. label.LineWrap = false;
  91. label.UseMarkup = true;
  92. label.UseUnderline = false;
  93. if(isManual)
  94. label.Markup = "<span weight=\"bold\" size=\"large\">User Specified</span>";
  95. else
  96. label.Markup = string.Format ("<span weight=\"bold\" size=\"large\">{0}</span>",
  97. serviceInfo.UserName);
  98. vbox.PackStart(label, true, true, 0);
  99. label = new Label();
  100. label.Justify = Gtk.Justification.Left;
  101. label.SetAlignment (0.0f, 0.5f);
  102. label.UseMarkup = true;
  103. label.UseUnderline = false;
  104. if(isManual) {
  105. label.LineWrap = true;
  106. label.Markup = "<span style=\"italic\" size=\"small\">Use this recipient to enter\ninformation manually.</span>";
  107. } else {
  108. label.LineWrap = false;
  109. label.Markup = string.Format ("<span size=\"small\">{0}</span>",
  110. serviceInfo.MachineName);
  111. }
  112. vbox.PackStart(label, true, true, 0);
  113. if(!isManual) {
  114. label = new Label();
  115. label.Justify = Gtk.Justification.Left;
  116. label.SetAlignment (0.0f, 0.5f);
  117. label.LineWrap = false;
  118. label.UseMarkup = true;
  119. label.UseUnderline = false;
  120. label.Markup = string.Format ("<span style=\"italic\" size=\"small\">{0}:{1}</span>",
  121. serviceInfo.Address, serviceInfo.Port);
  122. vbox.PackStart(label, true, true, 0);
  123. }
  124. hbox.ShowAll();
  125. outerVBox.PackStart (hbox, true, true, 0);
  126. progressBar = new ProgressBar ();
  127. progressBar.Orientation = ProgressBarOrientation.LeftToRight;
  128. progressBar.BarStyle = ProgressBarStyle.Continuous;
  129. progressBar.NoShowAll = true;
  130. outerVBox.PackStart (progressBar, true, false, 0);
  131. progressLabel = new Label ();
  132. progressLabel.UseMarkup = true;
  133. progressLabel.Xalign = 0;
  134. progressLabel.UseUnderline = false;
  135. progressLabel.LineWrap = true;
  136. progressLabel.Wrap = true;
  137. progressLabel.NoShowAll = true;
  138. progressLabel.Ellipsize = Pango.EllipsizeMode.End;
  139. outerVBox.PackStart (progressLabel, false, false, 0);
  140. outerVBox.ShowAll ();
  141. Add(outerVBox);
  142. TargetEntry[] targets = new TargetEntry[] {
  143. new TargetEntry ("text/uri-list", 0, (uint) DragTargetType.UriList) };
  144. this.DragDataReceived += DragDataReceivedHandler;
  145. Gtk.Drag.DestSet(this,
  146. DestDefaults.All | DestDefaults.Highlight,
  147. targets,
  148. Gdk.DragAction.Copy );
  149. }
  150. private void DragDataReceivedHandler (object o, DragDataReceivedArgs args)
  151. {
  152. //args.Context.
  153. switch(args.Info) {
  154. case (uint) DragTargetType.UriList:
  155. {
  156. UriList uriList = new UriList(args.SelectionData);
  157. string[] paths = uriList.ToLocalPaths();
  158. if(paths.Length > 0)
  159. {
  160. if(!isManual) {
  161. Application.EnqueueFileSend(serviceInfo, uriList.ToLocalPaths());
  162. } else {
  163. // Prompt for the info to send here
  164. }
  165. } else {
  166. // check for a tomboy notes
  167. foreach(Uri uri in uriList) {
  168. if( (uri.Scheme.CompareTo("note") == 0) &&
  169. (uri.Host.CompareTo("tomboy") == 0) ) {
  170. string[] files = new string[1];
  171. string tomboyID = uri.AbsolutePath.Substring(1);
  172. string homeFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
  173. string path = System.IO.Path.Combine(homeFolder, ".tomboy");
  174. path = System.IO.Path.Combine(path, tomboyID + ".note");
  175. files[0] = path;
  176. Logger.Debug("Go and get the tomboy note {0}", path);
  177. Application.EnqueueFileSend(serviceInfo, files);
  178. }
  179. }
  180. }
  181. break;
  182. }
  183. default:
  184. break;
  185. }
  186. //Logger.Debug("DragDataReceivedHandler called");
  187. Gtk.Drag.Finish (args.Context, true, false, args.Time);
  188. }
  189. private void OnSendFile (object sender, EventArgs args)
  190. {
  191. FileChooserDialog chooser = new FileChooserDialog(
  192. Catalog.GetString("File to Give"),
  193. null,
  194. FileChooserAction.Open
  195. );
  196. chooser.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
  197. chooser.AddButton(Stock.Cancel, ResponseType.Cancel);
  198. chooser.AddButton(Catalog.GetString("Give"), ResponseType.Ok);
  199. chooser.DefaultResponse = ResponseType.Ok;
  200. chooser.LocalOnly = true;
  201. if(chooser.Run() == (int)ResponseType.Ok) {
  202. Logger.Debug("Giving file {0}", chooser.Filename);
  203. if(!isManual) {
  204. Application.EnqueueFileSend(serviceInfo, chooser.Filenames);
  205. } else {
  206. // Prompt for the info to send here
  207. }
  208. }
  209. chooser.Destroy();
  210. }
  211. private void OnSendFolder (object sender, EventArgs args)
  212. {
  213. FileChooserDialog chooser = new FileChooserDialog(
  214. Catalog.GetString("Folder to Give"),
  215. null,
  216. FileChooserAction.SelectFolder
  217. );
  218. chooser.SetCurrentFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
  219. chooser.AddButton(Stock.Cancel, ResponseType.Cancel);
  220. chooser.AddButton(Catalog.GetString("Give"), ResponseType.Ok);
  221. chooser.DefaultResponse = ResponseType.Ok;
  222. chooser.LocalOnly = true;
  223. if(chooser.Run() == (int)ResponseType.Ok) {
  224. if(!isManual) {
  225. // Giver.Application.EnqueueFileSend(serviceInfo, chooser.Filename);
  226. } else {
  227. // Prompt for the info to send here
  228. }
  229. }
  230. chooser.Destroy();
  231. }
  232. #region Overrides
  233. protected override void OnClicked ()
  234. {
  235. Menu popupMenu = new Menu ();
  236. ImageMenuItem item;
  237. item = new ImageMenuItem ("Give a File...");
  238. item.Image = new Image(Gtk.Stock.File, IconSize.Button);
  239. item.Activated += OnSendFile;
  240. popupMenu.Add (item);
  241. item = new ImageMenuItem("Give a Folder...");
  242. item.Image = new Image(Gtk.Stock.Directory, IconSize.Button);
  243. item.Activated += OnSendFolder;
  244. popupMenu.Add (item);
  245. popupMenu.ShowAll();
  246. popupMenu.Popup ();
  247. }
  248. #endregion
  249. #region Event Handlers
  250. private void FileTransferStartedHandler (TransferStatusArgs args)
  251. {
  252. Gtk.Application.Invoke ( delegate {
  253. ProgressText = string.Format (
  254. Catalog.GetString ("Giving: {0}"),
  255. args.Name);
  256. progressBar.Text = string.Format (
  257. Catalog.GetString ("{0} of {1}"),
  258. args.CurrentCount,
  259. args.TotalCount);
  260. });
  261. if(!updateProgress) {
  262. updateProgress = true;
  263. GLib.Timeout.Add(50, UpdateProgressBar);
  264. }
  265. }
  266. private void TransferProgressHandler (TransferStatusArgs args)
  267. {
  268. progressFraction = ((double)args.TotalBytesTransferred) / ((double)args.TotalBytes);
  269. }
  270. private bool UpdateProgressBar()
  271. {
  272. if(updateProgress) {
  273. Gtk.Application.Invoke (delegate {
  274. progressBar.Fraction = progressFraction;
  275. });
  276. }
  277. return updateProgress;
  278. }
  279. private void TransferEndedHandler (TransferStatusArgs args)
  280. {
  281. updateProgress = false;
  282. Giver.Application.Instance.FileTransferStarted -= FileTransferStartedHandler;
  283. Giver.Application.Instance.TransferProgress -= TransferProgressHandler;
  284. Giver.Application.Instance.TransferEnded -= TransferEndedHandler;
  285. Gtk.Application.Invoke (delegate {
  286. progressBar.Hide ();
  287. ProgressText = string.Empty;
  288. progressLabel.Hide ();
  289. });
  290. }
  291. #endregion
  292. #region Public Properties
  293. public string ProgressText
  294. {
  295. set {
  296. progressLabel.Markup =
  297. string.Format ("<span size=\"small\" style=\"italic\">{0}</span>",
  298. value);
  299. }
  300. }
  301. #endregion
  302. #region Public Methods
  303. public void UpdateImage (Gdk.Pixbuf newImage)
  304. {
  305. Logger.Debug ("TargetService::UpdateImage called");
  306. this.image.FromPixbuf = newImage;
  307. }
  308. public void SetupTransferEventHandlers ()
  309. {
  310. Gtk.Application.Invoke ( delegate {
  311. progressBar.Show ();
  312. progressLabel.Show ();
  313. });
  314. Giver.Application.Instance.FileTransferStarted += FileTransferStartedHandler;
  315. Giver.Application.Instance.TransferProgress += TransferProgressHandler;
  316. Giver.Application.Instance.TransferEnded += TransferEndedHandler;
  317. }
  318. #endregion
  319. }
  320. }