Exemplo n.º 1
0
	    public GiverMenuItem(ServiceInfo serviceInfo) : base(false)
	    {
			this.serviceInfo = serviceInfo;

	        HBox hbox = new HBox(false, 10);
			Gtk.Image image;			
			if(serviceInfo.Photo != null)
				image = new Gtk.Image(serviceInfo.Photo);
			else
			 	image = new Gtk.Image(Utilities.GetIcon("giver-48", 48));
			hbox.PackStart(image, false, false, 0);
			VBox vbox = new VBox();
			hbox.PackStart(vbox, false, false, 0);
			Label label = new Label();
			label.Justify = Gtk.Justification.Left;
            label.SetAlignment (0.0f, 0.5f);
			label.LineWrap = false;
			label.UseMarkup = true;
			label.UseUnderline = false;
			label.Markup = string.Format ("<span weight=\"bold\" size=\"large\">{0}</span>",
                    						serviceInfo.UserName);
			vbox.PackStart(label, true, true, 0);

			label = new Label();
			label.Justify = Gtk.Justification.Left;
            label.SetAlignment (0.0f, 0.5f);
			label.LineWrap = false;
			label.UseMarkup = true;
			label.UseUnderline = false;
			label.Markup = string.Format ("<span size=\"small\">{0}</span>",
                    						serviceInfo.MachineName);

			vbox.PackStart(label, true, true, 0);

			label = new Label();
			label.Justify = Gtk.Justification.Left;
            label.SetAlignment (0.0f, 0.5f);
			label.LineWrap = false;
			label.UseMarkup = true;
			label.UseUnderline = false;
			label.Markup = string.Format ("<span style=\"italic\" size=\"small\">{0}:{1}</span>",
                    						serviceInfo.Address, serviceInfo.Port);

			vbox.PackStart(label, true, true, 0);
	        hbox.ShowAll();
	        Add(hbox);
	    }
Exemplo n.º 2
0
		public void QueueFileSend(ServiceInfo serviceInfo, string[] files)
		{
			lock(locker) {
				Logger.Debug("SEND: Queueing up {0} files to send", files.Length);
				SendingHolder sh = new SendingHolder();
				sh.files = files;
				sh.serviceInfo = serviceInfo;

				queue.Enqueue(sh);
			}
			resetEvent.Set();
		}
Exemplo n.º 3
0
		public static void GetPhoto(ServiceInfo serviceInfo)
		{
			UriBuilder urib = new UriBuilder("http", serviceInfo.Address.ToString(), (int)serviceInfo.Port);
			Logger.Debug("Sending request to URI: {0}", urib.Uri.ToString());
			System.Net.HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(urib.Uri);

			// Send request to send the file
			request.Method = "POST";
			request.Headers.Set(Protocol.Request, Protocol.Photo);
			request.ContentLength = 0;
			request.GetRequestStream().Close();

			// Read the response to the request
			HttpWebResponse response = (HttpWebResponse)request.GetResponse();

			if( response.StatusCode == HttpStatusCode.OK ) {
				Logger.Debug("Response was OK");

				byte[] buffer = new byte[response.ContentLength];
				int sizeRead = 0;
				int totalRead = 0;
				Stream stream = response.GetResponseStream();
				Logger.Debug("About to read photo of size {0}", response.ContentLength);

				try {

					do {
						sizeRead = stream.Read(buffer, totalRead, (int)(response.ContentLength - totalRead));
						totalRead += sizeRead;
						Logger.Debug("SizeRead = {0}, totalRead = {1}", sizeRead, totalRead);
					} while( (sizeRead > 0) && (totalRead < response.ContentLength) );

					Logger.Debug("We Read the photo and it's {0} bytes", totalRead);
					Logger.Debug("The content length is {0} bytes", response.ContentLength);

					stream.Close();
				} catch (Exception e) {
					Logger.Debug("Exception when reading file from stream: {0}", e.Message);
					Logger.Debug("Exception {0}", e);
				}

				serviceInfo.Photo = new Gdk.Pixbuf(buffer);

			} else {
				Logger.Debug("Unable to get the photo because {0}", response.StatusDescription);
			}
		}
Exemplo n.º 4
0
        private void SendFileThread()
        {
            while (running)
            {
                resetEvent.WaitOne();

                SendingHolder sh = null;
                resetEvent.Reset();

                lock (locker) {
                    if (queue.Count > 0)
                    {
                        sh = queue.Dequeue();
                    }
                }

                // if there was nothing to do, re-loop
                if (sh == null)
                {
                    continue;
                }

                ServiceInfo serviceInfo = sh.serviceInfo;

                // Calculate how many files to send and how big they are
                CalculateSendingHolderData(sh);
                //Logger.Debug("SEND: About to request send for {0} files at {1} bytes", sh.fileCount, sh.totalSize);

                UriBuilder urib = new UriBuilder("http", serviceInfo.Address.ToString(), (int)serviceInfo.Port);
                //Logger.Debug("SEND: Sending request to URI: {0}", urib.Uri.ToString());

                System.Net.HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(urib.Uri);

                // Send request to send the file
                request.Method = "GET";
                request.Headers.Set(Protocol.Request, Protocol.Send);
                request.Headers.Set(Protocol.UserName, Application.Preferences.UserName);
                request.Headers.Set(Protocol.Type, Protocol.ProtocolTypeFiles);
                request.Headers.Set(Protocol.Count, sh.fileCount.ToString());
//				*** START OF TOMBOY and TASQUE HACK ***

                if (sh.fileCount == 1)
                {
                    // Complete and total hack for TomBoy
                    Logger.Debug("The extension is: {0}", Path.GetExtension(sh.files[0]));
                    if (Path.GetExtension(sh.files[0]).CompareTo(".note") == 0)
                    {
                        Logger.Debug("I got a Note file");
                        try {
                            StreamReader reader  = new StreamReader(sh.files[0]);
                            string       noteXml = reader.ReadToEnd();
                            string       title   = null;
                            reader.Close();
                            XmlTextReader xml = new XmlTextReader(new StringReader(noteXml));
                            xml.Namespaces = false;
                            while (xml.Read())
                            {
                                switch (xml.NodeType)
                                {
                                case XmlNodeType.Element:
                                    switch (xml.Name)
                                    {
                                    case "title":
                                        title = xml.ReadString();
                                        break;
                                    }
                                    break;
                                }
                            }
                            if (title != null)
                            {
                                request.Headers.Set(Protocol.Name, title);
                                request.Headers.Set(Protocol.Type, Protocol.ProtocolTypeTomboy);
                            }
                            else
                            {
                                Logger.Debug("The node is null");
                                request.Headers.Set(Protocol.Name, Path.GetFileName(sh.files[0]));
                            }
                        } catch (Exception e) {
                            Logger.Debug("Exception getting note {0}", e);
                            request.Headers.Set(Protocol.Name, Path.GetFileName(sh.files[0]));
                        }
                    }
                    else if (Path.GetExtension(sh.files[0]).CompareTo(".tasque") == 0)
                    {
                        Logger.Debug("I got a task file");
                        try {
                            string name = null;
                            System.Xml.XmlDocument doc = new XmlDocument();
                            doc.Load(sh.files[0]);
                            XmlNode node = doc.SelectSingleNode("//name");
                            name = node.InnerText;

                            if (name != null)
                            {
                                request.Headers.Set(Protocol.Name, name);
                                request.Headers.Set(Protocol.Type, Protocol.ProtocolTypeTasque);
                            }
                            else
                            {
                                Logger.Debug("The node is null");
                                request.Headers.Set(Protocol.Name, Path.GetFileName(sh.files[0]));
                            }
                        } catch (Exception e) {
                            Logger.Debug("Exception getting task {0}", e);
                            request.Headers.Set(Protocol.Name, Path.GetFileName(sh.files[0]));
                        }
                    }
                    else
                    {
                        request.Headers.Set(Protocol.Name, Path.GetFileName(sh.files[0]));
                    }
                }

//				if(sh.fileCount == 1)
//					request.Headers.Set(Protocol.Name, Path.GetFileName(sh.files[0]));
                else
                {
                    request.Headers.Set(Protocol.Name, "many");
                }

                request.Headers.Set(Protocol.Size, sh.totalSize.ToString());

                //Logger.Debug("SEND: about to perform a GET for the file send request");
                HttpWebResponse response;

                // Read the response to the request
                try {
                    response = (HttpWebResponse)request.GetResponse();
                } catch (System.Net.WebException we) {
                    if (we.Response != null)
                    {
                        response = (HttpWebResponse)we.Response;
                    }
                    else
                    {
                        Logger.Debug("SEND: Exception in getting response {0}", we.Message);
                        continue;
                    }
                } catch (Exception e) {
                    Logger.Debug("SEND: Exception in request.GetResponse(): {0}", e);
                    continue;
                }

                Logger.Debug("SEND: Response Status code {0}", response.StatusCode);

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    //Logger.Debug("SEND: Response was OK");

                    string sessionID = response.Headers[Protocol.SessionID];
                    response.Close();
                    int  counter    = 0;
                    long entireSize = 0;

                    if (TransferStarted != null)
                    {
                        TransferStarted(new TransferStatusArgs(sh.fileCount, 0, "",
                                                               Protocol.ProtocolTypeFile, sh.totalSize,
                                                               0, request.ContentLength, 0, serviceInfo));
                    }


                    foreach (SendFilePair filePair in sh.filePairs)
                    {
                        request        = (HttpWebRequest)HttpWebRequest.Create(urib.Uri);
                        request.Method = "POST";
                        request.Headers.Set(Protocol.SessionID, sessionID);
                        request.Headers.Set(Protocol.Request, Protocol.Payload);
                        request.Headers.Set(Protocol.Type, Protocol.ProtocolTypeFile);
                        request.Headers.Set(Protocol.Count, counter.ToString());
                        // don't buffer the request being sent
                        request.AllowWriteStreamBuffering = false;
                        string fileName = Path.GetFileName(filePair.file);
                        request.Headers.Set(Protocol.Name, fileName);
                        request.Headers.Set(Protocol.RelativePath, filePair.relativePath);
                        request.Headers.Set(Protocol.TimeStamp,
                                            File.GetLastWriteTime(filePair.file).Ticks.ToString());

                        try {
                            System.IO.FileStream filestream = File.Open(filePair.file, FileMode.Open, FileAccess.Read);
                            request.ContentLength = filestream.Length;
                            Stream stream = request.GetRequestStream();

                            int    sizeRead  = 0;
                            int    totalRead = 0;
                            byte[] buffer    = new byte[8192];

                            if (FileTransferStarted != null)
                            {
                                FileTransferStarted(new TransferStatusArgs(sh.fileCount, counter, fileName,
                                                                           Protocol.ProtocolTypeFile, sh.totalSize,
                                                                           entireSize, request.ContentLength, totalRead, serviceInfo));
                            }


                            do
                            {
                                sizeRead    = filestream.Read(buffer, 0, 8192);
                                totalRead  += sizeRead;
                                entireSize += sizeRead;
                                if (sizeRead > 0)
                                {
                                    stream.Write(buffer, 0, sizeRead);
                                }

                                if (TransferProgress != null)
                                {
                                    TransferProgress(new TransferStatusArgs(sh.fileCount, counter, fileName,
                                                                            Protocol.ProtocolTypeFile, sh.totalSize,
                                                                            entireSize, request.ContentLength, totalRead, serviceInfo));
                                }
                            } while(sizeRead == 8192);
                            //Logger.Debug("SEND: We Read from the file {0} bytes", totalRead);
                            //Logger.Debug("SEND: The content length is {0} bytes", filestream.Length);

                            stream.Close();
                            filestream.Close();

                            // Read the response to the request
                            try {
                                response = (HttpWebResponse)request.GetResponse();
                            } catch (System.Net.WebException we) {
                                if (we.Response != null)
                                {
                                    response = (HttpWebResponse)we.Response;
                                }
                                else
                                {
                                    Logger.Debug("SEND: Exception in getting response {0}", we.Message);
                                    continue;
                                }
                            } catch (Exception e) {
                                Logger.Debug("SEND: Exception in request.GetResponse(): {0}", e);
                                continue;
                            }

                            response.Close();
                        } catch (Exception e) {
                            Logger.Debug("SEND: Exception when sending file: {0}", e.Message);
                            Logger.Debug("SEND: Exception {0}", e);
                        }
                        counter++;
                    }

                    if (TransferEnded != null)
                    {
                        TransferEnded(new TransferStatusArgs(sh.fileCount, counter, "",
                                                             Protocol.ProtocolTypeFile, sh.totalSize,
                                                             entireSize, request.ContentLength, 0, serviceInfo));
                    }



                    //Logger.Debug("RECEIVE: About to do a Gtk.Application.Invoke for the notify dude.");
                    Gtk.Application.Invoke(delegate {
                        string body = Services.PlatformService.GetString("{0} has received the file(s)!", serviceInfo.UserName);
                        //Logger.Debug("RECEIVE: Inside the Gtk.Application.Invoke dude");
                        Services.PlatformService.ShowMessage(Services.PlatformService.GetString("Done Giving Files."),
                                                             body, serviceInfo.Photo);

                        Services.PlatformService.PlaySoundFile(Path.Combine(Giver.Defines.SoundDir, "notify.wav"));
                    });
                }
                else
                {
                    //Logger.Debug("RECEIVE: About to do a Gtk.Application.Invoke for the notify dude.");
                    Gtk.Application.Invoke(delegate {
                        string body = Services.PlatformService.GetString(
                            "{0} declined your request to give files.", serviceInfo.UserName);
                        //Logger.Debug("RECEIVE: Inside the Gtk.Application.Invoke dude");
                        Services.PlatformService.ShowMessage(Services.PlatformService.GetString("Giving Was Declined"),
                                                             body, serviceInfo.Photo);

                        Services.PlatformService.PlaySoundFile(Path.Combine(Giver.Defines.SoundDir, "notify.wav"));
                    });
                }

                Logger.Debug("SEND: Done with Sending file");
            }
        }
Exemplo n.º 5
0
		static public void QueueResolve (ServiceInfo serviceInfo)
		{
			Logger.Debug ("QueueResolve called");
			lock (serviceLocker) {
				PhotoService.outstanding.Enqueue (serviceInfo);		
				PhotoService.photoServiceEvent.Set ();
			}
		}
Exemplo n.º 6
0
 public static void EnqueueFileSend(ServiceInfo serviceInfo, string[] files)
 {
     Giver.Application.Instance.sendingHandler.QueueFileSend(serviceInfo, files);
 }
Exemplo n.º 7
0
 public ServiceArgs(ServiceInfo serviceInfo)
 {
     this.serviceInfo = serviceInfo;
 }
Exemplo n.º 8
0
		public void OnAvatarUpdated (ServiceInfo serviceInfo)
		{
			Logger.Debug ("TargetWindow::OnAvatarUpdated - called");
			try {
				TargetService target = targets[serviceInfo.ID];
				target.UpdateImage (serviceInfo.Photo);
			} catch (Exception opu) {
				Logger.Debug (opu.Message);
			}
		}
Exemplo n.º 9
0
        private void PhotoServiceThread()
        {
            running = true;

            //Thread.Sleep (2000);
            while (running)
            {
                PhotoService.photoServiceEvent.WaitOne();
                if (running == false)
                {
                    continue;
                }

                ServiceInfo serviceInfo = null;
                //resetResolverEvent.Reset();

                lock (PhotoService.serviceLocker) {
                    if (PhotoService.outstanding.Count > 0)
                    {
                        serviceInfo = PhotoService.outstanding.Dequeue();
                    }
                }

                // if there was nothing to do, re-loop
                if (serviceInfo == null)
                {
                    continue;
                }

                Logger.Debug("Resolving photo for: {0}", serviceInfo.UserName);
                try {
                    if (serviceInfo.PhotoType.CompareTo(Preferences.Local) == 0)
                    {
                        SendingHandler.GetPhoto(serviceInfo);
                        serviceInfo.Photo = serviceInfo.Photo.ScaleSimple(48, 48, Gdk.InterpType.Bilinear);
                    }
                    else if (serviceInfo.PhotoType.CompareTo(Preferences.Gravatar) == 0)
                    {
                        string uri = Utilities.GetGravatarUri(serviceInfo.PhotoLocation);
                        serviceInfo.Photo = Utilities.GetPhotoFromUri(uri);
                        Logger.Debug("GetPhotoFromUri - finished");
                    }
                    else if (serviceInfo.PhotoType.CompareTo(Preferences.Uri) == 0)
                    {
                        serviceInfo.Photo = Utilities.GetPhotoFromUri(serviceInfo.PhotoLocation);
                        serviceInfo.Photo = serviceInfo.Photo.ScaleSimple(48, 48, Gdk.InterpType.Bilinear);
                    }
                    else
                    {
                        serviceInfo.Photo = Utilities.GetIcon("computer", 48);
                    }

                    // Call registered listeners
                    if (PhotoResolved != null)
                    {
                        PhotoResolved(serviceInfo);
                    }
                    else
                    {
                        Logger.Debug("No registered providers for PhotoResoved");
                    }
                } catch (Exception e) {
                    // FIXME:: Requeue and try again if the photo is
                    // coming from the network
                    Logger.Debug("Exception getting photo {0}", e);
                    //photoInfo.Photo = Utilities.GetIcon ("computer", 48);

                    // Failed to get the avatar requeue
                    lock (PhotoService.serviceLocker)
                        PhotoService.outstanding.Enqueue(serviceInfo);
                }
            }
        }
Exemplo n.º 10
0
		public TransferStatusArgs(int totalCount, int currentCount, string name, string type,
									long totalBytes, long totalBytesTransferred, long currentBytes,
									long currentBytesTransferred, ServiceInfo serviceInfo )
		{
			this.totalCount = totalCount;
			this.currentCount = currentCount;
			this.name = name;
			this.type = type;
			this.totalBytes = totalBytes;
			this.totalBytesTransferred = totalBytesTransferred;
			this.currentBytes = currentBytes;
			this.currentBytesTransferred = currentBytesTransferred;
			this.serviceInfo = serviceInfo;
		}
Exemplo n.º 11
0
		public TargetService(ServiceInfo serviceInfo)
		{
			this.serviceInfo = serviceInfo;
			isManual = false;
			Init();
		}
Exemplo n.º 12
0
        public ServiceArgs (ServiceInfo serviceInfo) 
		{
            this.serviceInfo = serviceInfo;
        }
Exemplo n.º 13
0
        private void OnServiceResolved (object o, ServiceResolvedEventArgs args)
		{
			IResolvableService service = o as IResolvableService;

			lock(locker) {
				if (services.ContainsKey(service.Name)) {
					// TODO: When making changes (like name or photo) at runtime becomes possible
					// this should allow updates to this info
					return; // we already have it somehow
            	}
			}

            ServiceInfo serviceInfo = new ServiceInfo (service.Name, service.HostEntry.AddressList[0], (ushort)service.Port);

			ITxtRecord record = service.TxtRecord;
			serviceInfo.UserName = record["User Name"].ValueString;
			serviceInfo.MachineName = record["Machine Name"].ValueString;
			serviceInfo.Version = record["Version"].ValueString;
			serviceInfo.PhotoType = record["PhotoType"].ValueString;
			serviceInfo.PhotoLocation = record["Photo"].ValueString;
			
			Logger.Debug("Setting default photo");
			serviceInfo.Photo = Utilities.GetIcon("blankphoto", 48);
			
			lock(locker) {
				services[serviceInfo.Name] = serviceInfo;
				
				if(serviceInfo.PhotoType.CompareTo(Preferences.Local) == 0 ||
					serviceInfo.PhotoType.CompareTo (Preferences.Gravatar) == 0 ||
					serviceInfo.PhotoType.CompareTo (Preferences.Uri) == 0) {
					// Queue the resolution of the photo
					PhotoService.QueueResolve (serviceInfo);
				}		
			}

            if (ServiceAdded != null)
			{
				Logger.Debug("About to call ServiceAdded");
                ServiceAdded (this, new ServiceArgs (serviceInfo));
				Logger.Debug("ServiceAdded was just called");
			} else {
				Logger.Debug("ServiceAdded was null and not called");
			}
        }
Exemplo n.º 14
0
		public static void EnqueueFileSend(ServiceInfo serviceInfo, string[] files)
		{
			Giver.Application.Instance.sendingHandler.QueueFileSend(serviceInfo, files);
		}
Exemplo n.º 15
0
		private void UpdatePhoto (ServiceInfo serviceInfo)
		{
			if (AvatarUpdated != null)
				AvatarUpdated (serviceInfo);
			else
				Logger.Debug ("No registered providers for AvatarUpdated");
		}
Exemplo n.º 16
0
		private void OnPhotoResolved (ServiceInfo serviceInfo)
		{
			Logger.Debug ("OnPhotoResolved called");
			Gtk.Application.Invoke( delegate {
				UpdatePhoto (serviceInfo);
			} );
		}