/regalandroid/src/main/java/net/dahanne/android/regalandroid/remote/G3Connection.java

https://github.com/dtsang29/ReGalAndroid · Java · 173 lines · 133 code · 20 blank · 20 comment · 9 complexity · 0615da7dee6ee5204bde90f1d557e681 MD5 · raw file

  1. /**
  2. * ReGalAndroid, a gallery client for Android, supporting G2, G3, etc...
  3. * URLs: https://github.com/anthonydahanne/ReGalAndroid , http://blog.dahanne.net
  4. * Copyright (c) 2010 Anthony Dahanne
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package net.dahanne.android.regalandroid.remote;
  19. import java.io.File;
  20. import java.io.InputStream;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import net.dahanne.gallery.commons.model.Album;
  27. import net.dahanne.gallery.commons.model.Picture;
  28. import net.dahanne.gallery.commons.remote.GalleryConnectionException;
  29. import net.dahanne.gallery.commons.remote.ImpossibleToLoginException;
  30. import net.dahanne.gallery.commons.remote.RemoteGallery;
  31. import net.dahanne.gallery3.client.business.G3Client;
  32. import net.dahanne.gallery3.client.business.exceptions.G3GalleryException;
  33. import net.dahanne.gallery3.client.model.Entity;
  34. import net.dahanne.gallery3.client.model.Item;
  35. import net.dahanne.gallery3.client.utils.G3ConvertUtils;
  36. public class G3Connection implements RemoteGallery {
  37. private final G3Client client;
  38. public G3Connection(String galleryUrl, String username, String password,
  39. String userAgent) {
  40. client = new G3Client(galleryUrl, userAgent);
  41. client.setUsername(username);
  42. client.setPassword(password);
  43. }
  44. @Override
  45. public int createNewAlbum(String galleryUrl, int parentAlbumName,
  46. String albumName, String albumTitle, String albumDescription)
  47. throws GalleryConnectionException {
  48. try {
  49. Entity albumToCreate = new Entity();
  50. albumToCreate.setName(albumName);
  51. albumToCreate.setTitle(albumTitle);
  52. albumToCreate.setParent(parentAlbumName);
  53. return client.createItem(albumToCreate, null);
  54. } catch (G3GalleryException e) {
  55. throw new GalleryConnectionException(e);
  56. }
  57. }
  58. @Override
  59. public Map<Integer, Album> getAllAlbums(String galleryUrl)
  60. throws GalleryConnectionException {
  61. Map<Integer, Album> map = new HashMap<Integer, Album>();
  62. try {
  63. List<Item> albumAndSubAlbums = client.getAlbumAndSubAlbums(1);
  64. for (Item item : albumAndSubAlbums) {
  65. Album itemToAlbum = G3ConvertUtils.itemToAlbum(item);
  66. map.put(itemToAlbum.getId(), itemToAlbum);
  67. }
  68. } catch (G3GalleryException e) {
  69. throw new GalleryConnectionException(e);
  70. }
  71. return map;
  72. }
  73. @Override
  74. public InputStream getInputStreamFromUrl(String umageUrl)
  75. throws GalleryConnectionException {
  76. try {
  77. return client.getPhotoInputStream(umageUrl);
  78. } catch (G3GalleryException e) {
  79. throw new GalleryConnectionException(e);
  80. }
  81. }
  82. @Override
  83. public Collection<Picture> getPicturesFromAlbum(int albumName)
  84. throws GalleryConnectionException {
  85. Collection<Picture> pictures = new ArrayList<Picture>();
  86. try {
  87. List<Item> picturesAsItems = client.getPictures(albumName);
  88. for (Item item : picturesAsItems) {
  89. pictures.add(G3ConvertUtils.itemToPicture(item));
  90. }
  91. } catch (G3GalleryException e) {
  92. throw new GalleryConnectionException(e);
  93. }
  94. return pictures;
  95. }
  96. @Override
  97. public void loginToGallery() throws ImpossibleToLoginException {
  98. try {
  99. client.getApiKey();
  100. } catch (G3GalleryException e) {
  101. throw new ImpossibleToLoginException(e);
  102. }
  103. }
  104. @Override
  105. public int uploadPictureToGallery(String galleryUrl, int albumName,
  106. File imageFile, String imageName, String summary, String description)
  107. throws GalleryConnectionException {
  108. int createItem = 0;
  109. try {
  110. Entity photoToCreate = new Entity();
  111. photoToCreate.setName(imageFile.getName());
  112. photoToCreate.setTitle(imageName);
  113. photoToCreate.setParent(albumName);
  114. createItem = client.createItem(photoToCreate, imageFile);
  115. } catch (G3GalleryException e) {
  116. throw new GalleryConnectionException(e);
  117. }
  118. return createItem;
  119. }
  120. @Override
  121. public Album getAlbumAndSubAlbumsAndPictures(int parentAlbumId)
  122. throws GalleryConnectionException {
  123. // dirty hack to load the root album, id 1 in G3
  124. if (parentAlbumId == 0) {
  125. parentAlbumId = 1;
  126. }
  127. Album parentAlbum = null;
  128. try {
  129. List<Item> albumAndSubAlbums = client
  130. .getAlbumAndSubAlbumsAndPictures(parentAlbumId);
  131. // the first is item is an album
  132. parentAlbum = G3ConvertUtils.itemToAlbum(albumAndSubAlbums.get(0));
  133. for (Item item : albumAndSubAlbums) {
  134. if (item == albumAndSubAlbums.get(0)) {
  135. // no need to add the first one, it is the parent
  136. continue;
  137. }
  138. if (item.getEntity().getType().equals("album")) {
  139. Album itemToAlbum = G3ConvertUtils.itemToAlbum(item);
  140. itemToAlbum.setParentName(parentAlbum.getName());
  141. itemToAlbum.setParent(parentAlbum);
  142. parentAlbum.getSubAlbums().add(itemToAlbum);
  143. } else {
  144. parentAlbum.getPictures().add(
  145. G3ConvertUtils.itemToPicture(item));
  146. }
  147. }
  148. } catch (G3GalleryException e) {
  149. throw new GalleryConnectionException(e);
  150. }
  151. return parentAlbum;
  152. }
  153. }