package com.onpositive.gae.tools;
import java.util.HashMap;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import com.onpositive.gae.tools.license.BadUserIdException;
public class SessionManager {
// private static SessionRunnable runnable;
private static final class SessionRunnable implements Runnable {
boolean okr;
String id;
String version;
public SessionRunnable(String appId, String version) {
this.id = appId;
this.version = version;
}
public void run() {
final Shell sh = new Shell(Display.getCurrent().getActiveShell(),
SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
sh.setLayout(new GridLayout(1, false));
sh.setText("Estabilishing AppWrench Session");
Label l = new Label(sh, SWT.NONE);
l.setText("You need to be logged into to google account, to estabilish AppWrench session to '"
+ id + "'.");
final Browser b = new Browser(sh, SWT.NONE);
b.setLayoutData(GridDataFactory.fillDefaults().hint(600, 600)
.grab(true, true).create());
Composite cm = new Composite(sh, SWT.NONE);
cm.setLayout(new GridLayout(2, false));
final Button ok = new Button(cm, SWT.NONE);
b.addProgressListener(new ProgressListener() {
public void completed(ProgressEvent event) {
String text = b.getText();
if (text.contains("Session estabilished")) {
String ts = "class=";
int indexOf = text.indexOf(ts);
if (indexOf != -1) {
String id = text.substring(indexOf + ts.length());
int p = id.indexOf('>');
if (p != -1) {
String acId = id.substring(0, p);
int indexOf2 = acId.indexOf(' ');
if (indexOf2 != -1) {
acId = acId.substring(0, indexOf2);
}
setSessionCode(SessionRunnable.this.id, acId);
ok.setEnabled(true);
}
}
}
}
public void changed(ProgressEvent event) {
}
});
ok.setText(" Ok ");
ok.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
okr = true;
sh.close();
}
});
ok.setEnabled(false);
Button cancel = new Button(cm, SWT.NONE);
cancel.setText("Cancel");
cancel.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
okr = false;
sh.close();
}
});
String uri = "http://" + version + ".latest." + id
+ ".appspot.com/appwrench/auth";
// uri = "http://localhost:5643/appwrench/auth";
cm.setLayoutData(GridDataFactory.fillDefaults()
.align(SWT.END, SWT.END).grab(false, false).create());
b.setUrl(uri);
sh.pack();
sh.open();
while (!sh.isDisposed()) {
Display.getCurrent().readAndDispatch();
}
}
public boolean isOk() {
return okr;
}
}
static HashMap<String, String> codes = new HashMap<String, String>();
public static String getSessionCode(String appId, boolean debug)
throws BadUserIdException {
if (appId == null || appId.length() == 0) {
return "";
}
String string = codes.get(appId);
String key = string;
if (key != null) {
int pos = key.length() - 1;
while (pos >= 0) {
char c = key.charAt(pos);
if (Character.isDigit(c) || c == '-') {
pos--;
continue;
} else {
break;
}
}
// String corePart = key.substring(0, pos + 1);
// if (!LicenseChecker.checkUserId(corePart,debug)) {
// return null;
// }
}
if (string != null && string.length() > 0 && string.charAt(0) == '\''
&& string.charAt(string.length() - 1) == '\'') {
return string.substring(1, string.length() - 1);
}
if (string != null && string.length() > 0 && string.charAt(0) == '\"'
&& string.charAt(string.length() - 1) == '\"') {
return string.substring(1, string.length() - 1);
}
return string;
}
public static void setSessionCode(String appId, String code) {
String logMessage = "Session established: (" + appId + "," + code + ")";
Activator.getDefault().getLog()
.log(new Status(0, Activator.PLUGIN_ID, logMessage));
System.out.println(logMessage);
codes.put(appId, code);
}
public static void removeSessionCode(String appId, String code) {
synchronized (SessionManager.class) {
if (codes == null) {
return;
}
String codeOld = codes.get(appId);
if (codeOld != null && codeOld.equals(code)) {
codes.remove(appId);
String logMessage = "Session code removed: (" + appId + ","
+ code + ")";
Activator.getDefault().getLog()
.log(new Status(0, Activator.PLUGIN_ID, logMessage));
System.out.println(logMessage);
}
}
}
public static boolean estabilishSession(String appId, String version) {
synchronized (SessionManager.class) {
if (codes.containsKey(appId)) {
return true;
}
SessionRunnable runnable = new SessionRunnable(appId, version);
Display.getDefault().syncExec(runnable);
return runnable.isOk();
}
}
}