File: DPAPService.cs

package info (click to toggle)
f-spot 0.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 21,784 kB
  • ctags: 16,078
  • sloc: cs: 108,718; sh: 17,053; xml: 13,852; ansic: 3,187; makefile: 2,324
file content (164 lines) | stat: -rw-r--r-- 4,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// DPAPService.cs : DPAP Server for f-spot
//
// Author:
// Andrzej Wytyczak-Partyka <iapart@gmail.com>
// Copyright (C) 2008 Andrzej Wytyczak-Partyka
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using FSpot;
using FSpot.Extensions;
using FSpot.Utils;
using FSpot.Widgets;

using System.IO;
using DPAP;
using Gtk;

namespace DPAP {
	

	public class DPAPService : IService
	{
		static ServiceDiscovery sd;
		public bool Start ()
		{
			Console.WriteLine ("Starting DPAP!");
			uint timer = Log.InformationTimerStart ("Starting DPAP");
			StartServer ();
			
			return true;
		}
		private void StartServer ()
		{
		Console.WriteLine ("Starting DPAP server");
			
			DPAP.Database database = new DPAP.Database ("DPAP");
			
			DPAP.Server server = new Server (System.Environment.UserName.ToString() + " f-spot photos");
			server.Port = 8770;
			server.AuthenticationMethod = AuthenticationMethod.None;
			int collision_count = 0;
			server.Collision += delegate {
				server.Name = System.Environment.UserName.ToString() + " f-spot photos" + "[" + ++collision_count + "]";
			};
            
			
			//FSpot.Photo photo = (FSpot.Photo) Core.Database.Photos.Get (1);			
			
			try {
				Album a = new Album ("test album");
				Tag t = Core.Database.Tags.GetTagByName ("Shared items");

				Tag []tags = {t};
				FSpot.Photo [] photos = Core.Database.Photos.Query (tags);
				int i=0;

				foreach (FSpot.Photo photo in photos) {
					string thumbnail_path = Gnome.Thumbnail.PathForUri (photo.DefaultVersionUri.ToString(), Gnome.ThumbnailSize.Large);
					FileInfo f = new FileInfo (thumbnail_path);
					DPAP.Photo p = new DPAP.Photo ();

					p.FileName = photo.Name;
					p.Thumbnail = thumbnail_path;
					p.ThumbSize = (int)f.Length;
					p.Path = photo.DefaultVersionUri.ToString ().Substring (7);
					f = new FileInfo (photo.DefaultVersionUri.ToString ().Substring (7));
					if (!f.Exists)
						continue;

					//if (++i > 2) break;
					Console.WriteLine ("Found photo " + p.Path  + ", thumb " + thumbnail_path);
					p.Title = f.Name;
					p.Size = (int)f.Length;
					p.Format = "JPEG";
					database.AddPhoto (p);
					a.AddPhoto (p);
				}		

				database.AddAlbum (a);
				Console.WriteLine ("Album count is now " + database.Albums.Count);
				server.AddDatabase (database);

				//server.GetServerInfoNode ();			
				try {
					server.Start();
				} catch (System.Net.Sockets.SocketException) {
					Console.WriteLine ("Server socket exception!");
					server.Port = 0;
					server.Start();
				}

				//DaapPlugin.ServerEnabledSchema.Set (true);

				//  if(!initial_db_committed) {
				server.Commit();

				//      initial_db_committed = true;
				//  }
			} catch (Exception e) {
				Console.WriteLine ("Failed starting dpap server \n{0}", e);
			}
		}
		
		public bool Stop ()
		{
			uint timer = Log.InformationTimerStart ("Stopping DPAP");
			if (sd != null) {
                sd.Stop ();
                sd.Found -= OnServiceFound;
                //locator.Removed -= OnServiceRemoved;
                sd = null;
            }
			return true;
		}

		private static void OnServiceFound (object o, ServiceArgs args)
		{
			Service service = args.Service;
			Client client;
//			ThreadAssist.Spawn (delegate {
        //        try {

			System.Console.WriteLine ("Connecting to {0} at {1}:{2}", service.Name, service.Address, service.Port);
		    client = new Client (service);
	
			
			/*foreach (Database d in client.Databases){

				Console.WriteLine ("Database " + d.Name);
				
				foreach (Album alb in d.Albums)
					Console.WriteLine ("\tAlbum: "+alb.Name + ", id=" + alb.getId () + " number of items:" + alb.Photos.Count);
				
				Console.WriteLine (d.Photos [0].FileName);
				foreach (DPAP.Photo ph in d.Photos)
				{
					if (ph != null)
					{
						Console.WriteLine ("\t\tFile: " + ph.Title + " format = " + ph.Format + "size=" + ph.Width +"x" +ph.Height + " ID=" + ph.Id);
						d.DownloadPhoto (ph,"./"+ph.Title);
					}
				}
				
			}*/
			//client.Logout ();
		//	Console.WriteLine ("Press <enter> to exit...");
		}		
	}
}