Example #1
0
		void runTask() {
			status(su, StatusUpdate.LEVEL_GENERIC, GRI18n.getString(MODULE, "newAlbm", new Object[] { album.getName(), g.toString() }));

			try {
				List<NameValuePair> formparams = new ArrayList<NameValuePair>();
				JSONObject jsonEntity = new JSONObject();
				jsonEntity.put("type", "album");
				if (album.getName() != null)
					jsonEntity.put("name", album.getName());
				if (album.getTitle() != null)
					jsonEntity.put("title", album.getTitle());
				if (album.getDescription() != null)
					jsonEntity.put("description", album.getDescription());
				formparams.add(new BasicNameValuePair("entity", jsonEntity.toJSONString()));

				BufferedReader entityReader = sendRequest(album.getParentAlbum().getUrl(), "post", formparams);

				String url = ((JSONObject) JSONValue.parse(entityReader)).get("url").toString();
				status(su, StatusUpdate.LEVEL_GENERIC, GRI18n.getString(MODULE, "crateAlbmOk"));
				album.setUrl(url);

				Log.log(Log.LEVEL_INFO, "Created album " + album.toString());
			} catch (IOException ioe) {
				Log.logException(Log.LEVEL_ERROR, MODULE, ioe);
				Object[] params2 = {ioe.toString()};
				error(su, GRI18n.getString(MODULE, "error", params2));
			}
		}
Example #2
0
	public boolean checkAuth() {
		try {
			sendRequest(g.getUrlString() + api + "item/1", "get", (HttpEntity) null);
		} catch (IOException e) {
			Log.logException(Log.LEVEL_CRITICAL, MODULE, e);
			return false;
		}

		return true;
	}
Example #3
0
	public BufferedReader sendRequest(String url, String verb, List<NameValuePair> formparams) throws IOException {
		if (formparams == null) {
			return sendRequest(url, verb, (HttpEntity) null);
		}

		UrlEncodedFormEntity entity = null;
		try {
			entity = new UrlEncodedFormEntity(formparams, "UTF-8");
			return sendRequest(url, verb, entity);
		} catch (UnsupportedEncodingException e) {
			Log.logException(Log.LEVEL_CRITICAL, MODULE, e);
			return null;
		}
	}
Example #4
0
		void runTask() {
			su.startProgress(StatusUpdate.LEVEL_BACKGROUND, 0, 10, GRI18n.getString(MODULE, "albmFtchng", new Object[] {g.toString()}), true);

			long startTime = System.currentTimeMillis();

			try {
				list();
			} catch (IOException e) {
				Log.logException(Log.LEVEL_CRITICAL, MODULE, e);
			}

			// tell the tree to reload
			g.reload();

			Log.log(Log.LEVEL_INFO, MODULE, "execution time for AlbumList: " + (System.currentTimeMillis() - startTime));

			su.stopProgress(StatusUpdate.LEVEL_BACKGROUND, GRI18n.getString(MODULE, "fetchComplete"));
		}
Example #5
0
		boolean uploadPicture(Picture p) {
			try {
				//transferListener.currentFile = p.toString();

				status(su, StatusUpdate.LEVEL_UPLOAD_ONE, GRI18n.getString(MODULE, "upPrep"));

				MultipartEntity entity = new MultipartEntity();

				Charset utf8 = Charset.forName("UTF-8");
				JSONObject jsonEntity = new JSONObject();
				jsonEntity.put("type", "photo");
				if (p.getName() != null)
					jsonEntity.put("name", p.getName());
				if (p.getTitle() != null)
					jsonEntity.put("title", p.getTitle());
				if (p.getDescription() != null)
					jsonEntity.put("description", p.getDescription());
				entity.addPart("entity", new StringBody(jsonEntity.toJSONString(), utf8));

				ContentBody body = new FileBody(p.getUploadSource());
				entity.addPart("file", body);

				BufferedReader entityReader = sendRequest(p.getParentAlbum().getUrl(), "post", entity);

				String url = ((JSONObject) JSONValue.parse(entityReader)).get("url").toString();
				status(su, StatusUpdate.LEVEL_UPLOAD_ONE, GRI18n.getString(MODULE, "upSucc"));
				p.setUrl(url);

				Log.log(Log.LEVEL_INFO, "Uploaded " + p.getUploadSource().toString() + " to " + url);

				return true;
				// set auto-rotate only if we do the rotation in GR, otherwise we'd be overriding the server setting
//				if (p.getAngle() != 0) {
//					opts[5] = new NVPair("auto_rotate", "no");
//				}

				// set up extra fields
//				if (p.getExtraFieldsMap() != null && p.getExtraFieldsMap().size() > 0) {
//					ArrayList optsList = new ArrayList(Arrays.asList(opts));
//
//					Iterator it = p.getExtraFieldsMap().keySet().iterator();
//					while (it.hasNext()) {
//						String name = (String) it.next();
//						String value = p.getExtraField(name);
//
//							optsList.add(new NVPair("extrafield." + name, value, utf8?"UTF-8":null));
//						}
//					}
//
//					opts = (NVPair[]) optsList.toArray(opts);
//				}

				// load and validate the response
//				Properties props = requestResponse(hdrs, data, g.getGalleryUrl(scriptName), true, su, this, transferListener);
//				if (props.getProperty("status").equals(GR_STAT_SUCCESS)) {
//					status(su, StatusUpdate.LEVEL_UPLOAD_ONE, GRI18n.getString(MODULE, "upSucc"));
//					String newItemName = props.getProperty("item_name");
//					if (newItemName != null) {
//						su.doneUploading(newItemName, picture);
//					}
//					return true;
//				} else {
//					Object[] params = {props.getProperty("status_text")};
//					error(su, GRI18n.getString(MODULE, "upErr", params));
//					return false;
//				}
			} catch (GR2Exception gr2e) {
				Log.logException(Log.LEVEL_ERROR, MODULE, gr2e);
				Object[] params = {gr2e.getMessage()};
				error(su, p.toString() + ": " + GRI18n.getString(MODULE, "error", params));
			} catch (IOException ioe) {
				Log.logException(Log.LEVEL_ERROR, MODULE, ioe);
				Object[] params = {ioe.toString()};
				error(su, p.toString() + ": " + GRI18n.getString(MODULE, "error", params));
			}

			return false;
		}
Example #6
0
		private void list() throws IOException {
			List<NameValuePair> formparams = new ArrayList<NameValuePair>();
			formparams.add(new BasicNameValuePair("type", "album"));
			formparams.add(new BasicNameValuePair("scope", "all"));

			BufferedReader entityReader = sendRequest(g.getUrlString() + api + "item/1", "get", formparams);

			JSONParser parser = new JSONParser();
			ListContentHandler lch = new ListContentHandler();

			HashMap<String,String> url2parentUrl = new HashMap<String,String>();
			HashMap<String,Album> url2album = new HashMap<String,Album>();
			ArrayList<Album> albums = new ArrayList<Album>();

			try {
				Album rootAlbum = g.createRootAlbum();
				rootAlbum.setUrl(g.getUrlString() + api + "item/1");
				rootAlbum.setSuppressEvents(true);
				lch.setAlbum(rootAlbum);
				parser.parse(entityReader, lch, true);
				rootAlbum.setSuppressEvents(false);
				// map album names to albums
				url2album.put(rootAlbum.getUrl(), rootAlbum);
				url2parentUrl.put(rootAlbum.getUrl(), lch.getParentUrl());

				while (!lch.isEnd()) {
					Album a = g.newAlbum();
					a.setSuppressEvents(true);
					lch.setAlbum(a);
					parser.parse(entityReader, lch, true);
					a.setSuppressEvents(false);

					albums.add(a);

					// map album names to albums
					url2album.put(a.getUrl(), a);
					url2parentUrl.put(a.getUrl(), lch.getParentUrl());
				}
			} catch (ParseException e) {
				Log.logException(Log.LEVEL_CRITICAL, MODULE, e);
			}

			Log.log(Log.LEVEL_TRACE, MODULE, "Created " + albums.size() + " albums");

			// link albums to parents
			for (Object o : url2parentUrl.keySet()) {
				String name = (String) o;
				String parentName = url2parentUrl.get(name);
				Album child = url2album.get(name);
				Album parent = url2album.get(parentName);

				if (child != null && parent != null) {
					parent.add(child);
				}
			}

			Log.log(Log.LEVEL_TRACE, MODULE, "Linked " + url2parentUrl.size() + " albums to their parents");

			// reorder
			Collections.sort(albums, new NaturalOrderComparator<Album>());
			Collections.reverse(albums);
			ArrayList<Album> orderedAlbums = new ArrayList<Album>();
			int depth = 0;
			while (!albums.isEmpty()) {
				Iterator<Album> it = albums.iterator();
				while (it.hasNext()) {
					Album a = it.next();

					try {
						if (a.getAlbumDepth() == depth) {
							it.remove();
							a.sortSubAlbums();

							Album parentAlbum = a.getParentAlbum();
							if (parentAlbum == null) {
								orderedAlbums.add(0, a);
							} else {
								int i = orderedAlbums.indexOf(parentAlbum);
								orderedAlbums.add(i + 1, a);
							}
						}
					} catch (IllegalArgumentException e) {
						it.remove();
						Log.log(Log.LEVEL_TRACE, MODULE, "Gallery server album list is corrupted: " +
								"album " + a.getName() + " has a bad containment hierarchy.");
					}
				}

				depth++;
			}

			Log.log(Log.LEVEL_TRACE, MODULE, "Ordered " + orderedAlbums.size() + " albums");

			status(su, StatusUpdate.LEVEL_BACKGROUND, GRI18n.getString(MODULE, "ftchdAlbms"));
		}