예제 #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));
			}
		}
예제 #2
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"));
		}
예제 #3
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;
		}
예제 #4
0
		void runTask() {
			ArrayList<Picture> pictures = g.getAllUploadablePictures();

			// get total file size
			long totalSize = 0;
			Iterator<Picture> iter = pictures.iterator();
			while (iter.hasNext()) {
				Picture p = iter.next();
				totalSize += p.getFileSize();
			}

//			transferListener = new MyTransferListener(su);
//			transferListener.sizeAllFiles = totalSize;
//			transferListener.numberAllFiles = pictures.size();

			su.startProgress(StatusUpdate.LEVEL_UPLOAD_ALL, 0, 100, GRI18n.getString(MODULE, "upPic"), false);

			if (su instanceof UploadProgress) {
				((UploadProgress) su).setCancelListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						su.updateProgressStatus(StatusUpdate.LEVEL_UPLOAD_ALL, GRI18n.getString(MODULE, "upStop"));
						su.setUndetermined(StatusUpdate.LEVEL_UPLOAD_ALL, true);
						interrupt();
						long startTime = System.currentTimeMillis();

						while (!terminated && System.currentTimeMillis() < startTime + 10000) {
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e1) {
							}
						}

						if (!terminated) {
							Log.log(Log.LEVEL_ERROR, "Thread would not terminate properly: killing it");
							thread.stop();

							// since we killed the thread, it's not going to clean up after itself
							cleanUp();
						}

						((UploadProgress) su).done();
					}
				});
			}

			// upload each file, one at a time
			boolean allGood = true;
			//int uploadedCount = 0;
			iter = pictures.iterator();
			while (iter.hasNext() /*&& allGood*/ && !interrupt) {
				Picture p = iter.next();

				/*Object[] params = {
						p.toString(),
						new Integer((uploadedCount + 1)),
						new Integer(pictures.size()),
						new Integer((int) ((transferListener.sizeFilesDone + transferListener.sizeThisFile) / 1024 / 1024)),
						new Integer((int) (transferListener.sizeAllFiles / 1024 / 1024)),
						transferListener.getProjectedTimeLeft()
				};
				su.updateProgressStatus(StatusUpdate.LEVEL_UPLOAD_ALL, GRI18n.getString(MODULE, "upStatus", params));*/

				allGood = uploadPicture(p);

				//su.updateProgressValue(StatusUpdate.LEVEL_UPLOAD_ALL, ++uploadedCount);

				if (allGood) {
					p.getParentAlbum().removePicture(p);
				}
			}

			if (allGood) {
				su.stopProgress(StatusUpdate.LEVEL_UPLOAD_ALL, GRI18n.getString(MODULE, "upComplete"));

				if (su instanceof UploadProgress) {
					if (((UploadProgress) su).isShutdown()) {
						GalleryRemote._().getCore().shutdown(true);
					}
				}

				g.setDirty(false);

				GalleryRemote._().getCore().flushMemory();
			} else {
				su.stopProgress(StatusUpdate.LEVEL_UPLOAD_ALL, GRI18n.getString(MODULE, "upFailed"));
			}
		}
예제 #5
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"));
		}