private void RefreshPhotos(string revquery)
        {
            foreach (Album pl in albums)
            {
                //Console.WriteLine ("Refreshing photos in album " + pl.Name);
                byte [] photos_data = client.Fetcher.Fetch(String.Format("/databases/{0}/containers/{1}/items", id, pl.getId()),
                                                           photo_query);
                ContentNode photos_node = ContentParser.Parse(client.Bag, photos_data);
                //photos_node.Dump ();
                //if (IsUpdateResponse (photos_node))
                //   return;

                // handle photo additions/changes
                foreach (ContentNode photoNode in (ContentNode [])photos_node.GetChild("dmap.listing").Value)
                {
                    // DEBUG data
                    //photoNode.Dump ();
                    Photo photo = Photo.FromNode(photoNode);

                    Photo existing = LookupPhotoById(photo.Id);
                    pl.AddPhoto(photo);
                    if (existing == null)
                    {
                        Console.WriteLine("adding " + photo.Title + " to album " + pl.Name);

                        AddPhoto(photo);
                    }
                    else
                    {
                        Console.WriteLine("updating " + existing.Title);
                        existing.Update(photo);
                    }
                }

                if ((byte)photos_node.GetChild("dmap.updatetype").Value == 1)
                {
                    // handle photo deletions
                    ContentNode delete_list = photos_node.GetChild("dmap.deletedidlisting");

                    if (delete_list != null)
                    {
                        foreach (ContentNode deleted in (ContentNode [])delete_list.Value)
                        {
                            Photo photo = LookupPhotoById((int)deleted.Value);

                            if (photo != null)
                            {
                                RemovePhoto(photo);
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        private int WaitForRevision(int currentRevision)
        {
            ContentNode rev_node = ContentParser.Parse(bag, fetcher.Fetch("/update",
                                                                          "revision-number=" + currentRevision));

            return((int)rev_node.GetChild("dmap.serverrevision").Value);
        }
        public void DownloadPhoto(Photo photo, string dest)
        {
/*            BinaryWriter writer = new BinaryWriter (File.Open (dest, FileMode.Create));
 *                      MemoryStream data = new MemoryStream ();
 *          try {
 *              long len;
 *              using (BinaryReader reader = new BinaryReader (StreamPhoto (photo, out len))) {
 *                  int count = 0;
 *                  byte [] buf = new byte [chunk_length];
 *
 *                                      // Skip the header
 *                                      //count = reader.Read (buf,0,89);
 *
 *                                      //if (count < 89)
 *                                      //	count+=reader.Read (buf,0,89-count);
 *
 *                                      while (true) {
 *                  buf = reader.ReadBytes (8192);
 *                  if (buf.Length == 0)
 *                      break;
 *
 *                  data.Write (buf, 0, buf.Length);
 *                                      //Console.Write (buf.);
 *                                      }
 *
 */
/*                    do {
 *                      count = reader.Read (buf, 0, chunk_length);
 *                      writer.Write (buf, 0, count);
 *                                              data.Write (buf, 0, count);
 *                  } while (count != 0);
 */
            /*data.Flush ();
             *
             * ContentNode node = ContentParser.Parse (client.Bag, data.GetBuffer ());
             * node.Dump ();
             * reader.Close ();
             *
             *  }
             * } finally {
             * data.Close ();
             *
             *  writer.Close ();
             * }*/
            // maybe use FetchResponse to get a stream and feed it to pixbuf?
            byte [] photos_data = client.Fetcher.Fetch(String.Format("/databases/{0}/items", id),
                                                       String.Format("meta=dpap.thumb,dpap.filedata&query=('dmap.itemid:{0}')", photo.Id));
            ContentNode node = ContentParser.Parse(client.Bag, photos_data);

            // DEBUG
            Console.WriteLine("About to dump the photo!");
            node.Dump();
            ContentNode filedata_node = node.GetChild("dpap.filedata");

            Console.WriteLine("Photo starts at index " + filedata_node.Value);
            BinaryWriter writer = new BinaryWriter(File.Open(dest, FileMode.Create));

            int count = 0;
            int off   = System.Int32.Parse(filedata_node.Value.ToString());

            byte []      photo_buf;
            MemoryStream data = new MemoryStream();

            writer.Write(photos_data, (int)off, (int)photos_data.Length - off);
            data.Position = 0;
            //	Gdk.Pixbuf pb = new Gdk.Pixbuf (data);
            data.Close();
            Console.Write("Written " + count + " out of " + (photos_data.Length - off));
        }
        private void RefreshAlbums(string revquery)
        {
            byte [] albums_data;

            try {
                albums_data = client.Fetcher.Fetch(String.Format("/databases/{0}/containers", id), "meta=dpap.aspectratio,dmap.itemid,dmap.itemname,dpap.imagefilename,dpap.imagefilesize,dpap.creationdate,dpap.imagepixelwidth,dpap.imagepixelheight,dpap.imageformat,dpap.imagerating,dpap.imagecomments,dpap.imagelargefilesize&type=photo");
            } catch (WebException) {
                return;
            }

            ContentNode albums_node = ContentParser.Parse(client.Bag, albums_data);

            // DEBUG data
            albums_node.Dump();
            Console.WriteLine("after dump!");

            if (IsUpdateResponse(albums_node))
            {
                return;
            }

            // handle album additions/changes
            ArrayList plids = new ArrayList();

            if (albums_node.GetChild("dmap.listing") == null)
            {
                return;
            }

            foreach (ContentNode albumNode in (ContentNode [])albums_node.GetChild("dmap.listing").Value)
            {
                // DEBUG
                Console.WriteLine("foreach loop");
                Album pl = Album.FromNode(albumNode);
                if (pl != null)
                {
                    plids.Add(pl.Id);
                    Album existing = LookupAlbumById(pl.Id);

                    if (existing == null)
                    {
                        AddAlbum(pl);
                    }
                    else
                    {
                        existing.Update(pl);
                    }
                }
            }
            // DEBUG
            Console.WriteLine("delete albums that don't exist");
            // delete albums that no longer exist
            foreach (Album pl in new List <Album> (albums))
            {
                if (!plids.Contains(pl.Id))
                {
                    RemoveAlbum(pl);
                }
            }

            plids = null;
            // DEBUG
            Console.WriteLine("Add/remove photos in the albums");
            // add/remove photos in the albums
            foreach (Album pl in albums)
            {
                byte [] album_photos_data = client.Fetcher.Fetch(String.Format("/databases/{0}/containers/{1}/items",
                                                                               id, pl.Id), "meta=dpap.aspectratio,dmap.itemid,dmap.itemname,dpap.imagefilename,dpap.imagefilesize,dpap.creationdate,dpap.imagepixelwidth,dpap.imagepixelheight,dpap.imageformat,dpap.imagerating,dpap.imagecomments,dpap.imagelargefilesize&type=photo");
                ContentNode album_photos_node = ContentParser.Parse(client.Bag, album_photos_data);

                if (IsUpdateResponse(album_photos_node))
                {
                    return;
                }

                if ((byte)album_photos_node.GetChild("dmap.updatetype").Value == 1)
                {
                    // handle album photo deletions
                    ContentNode delete_list = album_photos_node.GetChild("dmap.deletedidlisting");

                    if (delete_list != null)
                    {
                        foreach (ContentNode deleted in (ContentNode [])delete_list.Value)
                        {
                            int index = pl.LookupIndexByContainerId((int)deleted.Value);

                            if (index < 0)
                            {
                                continue;
                            }

                            pl.RemoveAt(index);
                        }
                    }
                }

                // add new photos, or reorder existing ones

                int plindex = 0;
                foreach (ContentNode pl_photo_node in (ContentNode [])album_photos_node.GetChild("dmap.listing").Value)
                {
                    Photo plphoto      = null;
                    int   container_id = 0;
                    Photo.FromAlbumNode(this, pl_photo_node, out plphoto, out container_id);

                    if (pl [plindex] != null && pl.GetContainerId(plindex) != container_id)
                    {
                        pl.RemoveAt(plindex);
                        pl.InsertPhoto(plindex, plphoto, container_id);
                    }
                    else if (pl [plindex] == null)
                    {
                        pl.InsertPhoto(plindex, plphoto, container_id);
                    }

                    plindex++;
                }
            }
        }
Exemple #5
0
        public static ContentNode Parse(ContentCodeBag bag, byte [] buffer, string root,
                                        ref int offset)
        {
            ContentNode node = new ContentNode();
            int         num  = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset));
            ContentCode code;

            // This is a fix for iPhoto '08 which gives wrong content-type for dpap.databasecontainers (aply)
            if (num == 1634757753)
            {
                code      = new ContentCode();
                code.Name = "dpap.databasecontainers";
                code.Type = ContentType.Container;
            }
            else
            {
                code = bag.Lookup(num);
            }

            if (code.Name.Equals("dpap.filedata"))
            {
                code.Type = ContentType.FileData;
            }

            if (code.Equals(ContentCode.Zero))
            {
                // probably a buggy server.  fallback to our internal code bag
                Console.WriteLine("fallback to internal code bag");
                Console.WriteLine("Code number: " + num);
                throw new Exception("Content code not found!");
            }

            int length = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 4));

            if (code.Equals(ContentCode.Zero))
            {
                throw new ContentException(String.Format("Failed to find content code for '{0}'.  Data length is {1}",
                                                         ContentCodeBag.GetStringFormat(num), length));
            }

            node.Name = code.Name;

            Console.WriteLine("name = " + node.Name + "Code=" + code.Type.ToString() + " num=" + num);

            switch (code.Type)
            {
            case ContentType.Char:
                node.Value = (byte)buffer [offset + 8];
                break;

            case ContentType.Short:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, offset + 8));
                break;

            case ContentType.SignedLong:
            case ContentType.Long:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 8));
                break;

            case ContentType.LongLong:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(buffer, offset + 8));
                break;

            case ContentType.String:
                node.Value = Encoding.UTF8.GetString(buffer, offset + 8, length);
                break;

            case ContentType.Date:
                node.Value = Utility.ToDateTime(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 8)));
                break;

            case ContentType.Version:
                int major = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, offset + 8));
                int minor = (int)buffer [offset + 10];
                int micro = (int)buffer [offset + 11];

                node.Value = new Version(major, minor, micro);
                break;

            case ContentType.Container:
                node.Value = ParseChildren(bag, buffer, offset + 8, length);
                break;

            case ContentType.FileData:
                node.Value = offset + 8;
                break;

            default:
                throw new ContentException(String.Format("Unknown content type '{0}' for '{1}'",
                                                         code.Type, code.Name));
            }

            offset += length + 8;

            if (root != null)
            {
                ContentNode root_node = node.GetChild(root);

                if (root_node == null)
                {
                    throw new ContentException(String.Format("Could not find root node '{0}'", root));
                }

                return(root_node);
            }
            else
            {
                return(node);
            }
        }
        public static ContentNode Parse(ContentCodeBag bag, byte [] buffer, string root,
                                         ref int offset)
        {
            ContentNode node = new ContentNode ();
            int num = IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, offset));
            ContentCode code;
            // This is a fix for iPhoto '08 which gives wrong content-type for dpap.databasecontainers (aply)
            if (num == 1634757753) {
                code = new ContentCode ();
                code.Name = "dpap.databasecontainers";
                code.Type = ContentType.Container;
            }
            else
                code = bag.Lookup (num);

            if (code.Name.Equals ("dpap.filedata"))
                code.Type = ContentType.FileData;

            if (code.Equals (ContentCode.Zero)) {
                // probably a buggy server.  fallback to our internal code bag
                Console.WriteLine ("fallback to internal code bag");
                Console.WriteLine ("Code number: "+num);
                throw new Exception ("Content code not found!");
            }

            int length = IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, offset + 4));

            if (code.Equals (ContentCode.Zero)) {
                throw new ContentException (String.Format ("Failed to find content code for '{0}'.  Data length is {1}",
                                                           ContentCodeBag.GetStringFormat (num), length));
            }

            node.Name = code.Name;

            Console.WriteLine ("name = " + node.Name + "Code=" +code.Type.ToString () + " num=" +num);

            switch (code.Type) {
                case ContentType.Char:
                    node.Value = (byte) buffer [offset + 8];
                    break;
                case ContentType.Short:
                    node.Value = IPAddress.NetworkToHostOrder (BitConverter.ToInt16 (buffer, offset + 8));
                    break;
                case ContentType.SignedLong:
                case ContentType.Long:
                    node.Value = IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, offset + 8));
                    break;
                case ContentType.LongLong:
                    node.Value = IPAddress.NetworkToHostOrder (BitConverter.ToInt64 (buffer, offset + 8));
                    break;
                case ContentType.String:
                    node.Value = Encoding.UTF8.GetString (buffer, offset + 8, length);
                    break;
                case ContentType.Date:
                    node.Value = Utility.ToDateTime (IPAddress.NetworkToHostOrder (BitConverter.ToInt32 (buffer, offset + 8)));
                    break;
                case ContentType.Version:
                    int major = IPAddress.NetworkToHostOrder (BitConverter.ToInt16 (buffer, offset + 8));
                    int minor = (int) buffer [offset + 10];
                    int micro = (int) buffer [offset + 11];

                    node.Value = new Version (major, minor, micro);
                    break;
                case ContentType.Container:
                    node.Value = ParseChildren (bag, buffer, offset + 8, length);
                    break;
                case ContentType.FileData:
                    node.Value = offset+8;
                    break;
                default:
                    throw new ContentException (String.Format ("Unknown content type '{0}' for '{1}'",
                                                               code.Type, code.Name));
            }

            offset += length + 8;

            if (root != null) {
                ContentNode root_node = node.GetChild (root);

                if (root_node == null)
                    throw new ContentException (String.Format ("Could not find root node '{0}'", root));

                return root_node;
            } else
                return node;
        }
Exemple #7
0
 private void ParseSessionId(ContentNode node)
 {
     fetcher.SessionId = (int)node.GetChild("dmap.sessionid").Value;
 }