package mesfavoris.gdrive.connection.auth;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.java6.auth.oauth2.VerificationCodeReceiver;

import mesfavoris.gdrive.StatusHelper;

public class AuthorizationCodeEclipseApp extends AuthorizationCodeInstalledApp {
	private final IProgressMonitor monitor;
	
	public AuthorizationCodeEclipseApp(AuthorizationCodeFlow flow,
			VerificationCodeReceiver receiver, IProgressMonitor monitor) {
		super(flow, receiver);
		this.monitor = monitor;
	}

	@Override
	protected void onAuthorization(
			AuthorizationCodeRequestUrl authorizationUrl)
			throws IOException {
		monitor.subTask("Please open the following address in your browser:"
				+ authorizationUrl);
		openInExternalBrowser(URI.create(authorizationUrl.build()));
	}

	private void openInExternalBrowser(URI url) {
		String browserId = "gdrive-authorize";
		try {
			IWorkbenchBrowserSupport browserSupport = PlatformUI
					.getWorkbench().getBrowserSupport();

			IWebBrowser browser = browserSupport
					.createBrowser(
							IWorkbenchBrowserSupport.AS_EXTERNAL,
							browserId,
							"Authorizes the application to access user's protected data on GDrive",
							null);
			browser.openURL(url.toURL());
		} catch (PartInitException e) {
			StatusHelper.logError("Could not open browser", e);
		} catch (MalformedURLException e) {
			StatusHelper.logError("Could not open browser", e);
		}

	}	
	
	public static class Provider implements IAuthorizationCodeInstalledAppProvider {

		@Override
		public AuthorizationCodeInstalledApp get(AuthorizationCodeFlow flow, VerificationCodeReceiver receiver,
				IProgressMonitor monitor) {
			return new AuthorizationCodeEclipseApp(flow, receiver, monitor);
		}
		
	}
	
}

@@ -176,16 +176,172 @@
 onitor;%0A
+import org.eclipse.swt.dnd.Clipboard;%0Aimport org.eclipse.swt.dnd.TextTransfer;%0Aimport org.eclipse.swt.dnd.Transfer;%0Aimport org.eclipse.swt.widgets.Display;%0A
 import o
@@ -1287,168 +1287,738 @@
 %7B%0A%09%09
-monitor.subTask(%22Please open the following address in your browser:%22%0A%09%09%09%09+ authorizationUrl);%0A%09%09openInExternalBrowser(URI.create(authorizationUrl.build()));%0A%09%7D%0A
+String authorizationUrlAsString = authorizationUrl.build();%0A%09%09Display display = Display.getDefault();%0A%09%09display.syncExec(() -%3E copyTextToClipboard(display, authorizationUrlAsString));%0A%09%09monitor.subTask(%22Please authorize the application in your browser. Url has been copied to clipboard%22);%09%09%0A%09%09openInExternalBrowser(URI.create(authorizationUrlAsString));%0A%09%7D%0A%0A%09private void copyTextToClipboard(Display display, String text) %7B%0A%09%09Clipboard clipboard = new Clipboard(display);%0A%09%09try %7B%0A%09%09%09TextTransfer textTransfer = TextTransfer.getInstance();%0A%09%09%09Transfer%5B%5D transfers = new Transfer%5B%5D %7B textTransfer %7D;%0A%09%09%09Object%5B%5D data = new Object%5B%5D %7B text %7D;%0A%09%09%09clipboard.setContents(data, transfers);%0A%09%09%7D finally %7B%0A%09%09%09clipboard.dispose();%0A%09%09%7D%0A%09%7D%09%0A%09
 %0A%09pr

Copy the authorization url in clipboard