/**
* Copyright (C) Gadglet .
*
* This file is part of Gadglet
*
* Gadglet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Gadglet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
*/
package com.gadglet.client.gwt.google;
import com.gadglet.client.gwt.GadgetUtils;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.PreferencesFeature;
import com.google.gwt.gadgets.client.PreferencesProvider;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
* This class will enable the Gadget OAuth popup
* process
*
*/
public class OauthApprovalPopUp {
private GoogleRequest activeRequest =null;
private String winParams = null;
private GoogleFeedHandler activeHandler;
/**
* Set parameters to open the registration window default "width=600,height=500"
* @param winParams - parameters to open the registration
*/
public void setRegWinParams(String winParams){
this.winParams = winParams;
}
private String getRegWinParams(){
return this.winParams;
}
private DialogBox dialogBox;
private HTML detailsHTML;
final Button dialogButton = new Button();
protected final PreferencesFeature prefs = PreferencesProvider.get();
public OauthApprovalPopUp() {
dialogBox = new DialogBox();
dialogBox.setAnimationEnabled(true);
detailsHTML = new HTML();
detailsHTML.setVisible(true);
VerticalPanel dialogVPanel = new VerticalPanel();
dialogVPanel.setStylePrimaryName("error-panel");
dialogVPanel
.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
ScrollPanel panel = new ScrollPanel(detailsHTML);
panel.setSize("200px", "100px");
FlowPanel buttons = new FlowPanel();
buttons.add(dialogButton);
dialogVPanel.add(panel);
dialogVPanel.add(buttons);
dialogBox.setWidget(dialogVPanel);
}
/**
* initiate the OAuth process
* @param url
* @param request
* @param handler
* @param serviceApprovalMsg
*/
public void approveOAuth(final String url,GoogleRequest request, GoogleFeedHandler handler, String serviceApprovalMsg) {
if(serviceApprovalMsg == null)
serviceApprovalMsg = prefs.getMsg("gadgetMsgPleaseApproveGoogle");
else
serviceApprovalMsg = prefs.getMsg(serviceApprovalMsg);
dialogButton.setVisible(true);
dialogBox.setText(prefs.getMsg("gadgetLabelSecurityApproval"));
detailsHTML.setHTML(serviceApprovalMsg);
dialogButton.setText(prefs.getMsg("gadgetLabelApprove"));
this.activeRequest = request;
this.activeHandler= handler;
dialogButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
open(url,null);
dialogButton.setVisible(false);
detailsHTML.setHTML(prefs.getMsg("gadgetMsgUserApprovingPlatform"));
}
});
if (GadgetUtils.isCanvasView())
GadgetUtils.dialogBoxTop(dialogBox);
else
dialogBox.center();
}
/**
* open window, then start a process to watch for it to close
*
* @param url - oauth approval url
*/
private void open(String url, String winParam) {
if(winParam!=null && !winParam.isEmpty())
openWindow(url,winParam);
else
openWindow(url,"width=600,height=500");
loop();
}
/**
* open new window
* "_blank",
* @param url - oauth approval url
* @return
*/
private final native void openWindow(String url, String winParam) /*-{
$wnd.winHandle = $wnd.open(url, "_blank",winParam);
}-*/;
/**
* is popup window still open
*
* @return - boolean
*/
private final native boolean isWindowClosed() /*-{
var b = false;
if ($wnd.winHandle.closed == true) {
b = true;
$wnd.winHandle = null;
}
return b;
}-*/;
/**
* loop, watching for window to close
*
*/
private void loop() {
Timer t = new Timer() {
@Override
public void run() {
if (isWindowClosed() == true) {
setWindowClosed();
return;
}
loop();
}
};
t.schedule(100);
}
/**
* call this when the window closes
*/
public void setWindowClosed() {
dialogBox.hide();
activeRequest.callFeed(activeHandler);
}
}