Skip to content

Commit

Permalink
Revamped the File menu which now adapts when logging in to a cloud dr…
Browse files Browse the repository at this point in the history
…ive.
  • Loading branch information
98ahni committed Oct 25, 2024
1 parent 6bcf7af commit 269f84f
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 95 deletions.
9 changes: 7 additions & 2 deletions Source/Extensions/Dropbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

EM_JS(bool, db_open_auth_popup, (emscripten::EM_VAL token_callback), {
const callback_func = Module[Emval.toValue(token_callback)];
global_db_auth.getAuthenticationUrl(window.location.href, undefined, 'code', 'offline', ['files.content.write', 'files.content.read'], undefined, true).then(authUrl => {
global_db_auth.getAuthenticationUrl(window.location.href, undefined, 'code', 'offline', ['account_info.read', 'files.content.write', 'files.content.read'], undefined, true).then(authUrl => {
const popup = window.open(authUrl, 'Log In with Dropbox', 'width=520,height=600');
const message_func = function(msg){
popup.close();
Expand All @@ -20,7 +20,12 @@ EM_JS(bool, db_open_auth_popup, (emscripten::EM_VAL token_callback), {
global_db_auth.setRefreshToken(res.result.refresh_token);
global_db_auth.setAccessTokenExpiresAt(res.result.expires_in);
global_db_api = new Dropbox.Dropbox({auth: global_db_auth});
callback_func(Emval.toHandle(global_db_auth.getAccessTokenExpiresAt()));
global_db_api.usersGetCurrentAccount().then((user_res)=>{
callback_func(
Emval.toHandle(Date.now() + (global_db_auth.getAccessTokenExpiresAt() * 1000)),
Emval.toHandle(user_res.result.name.display_name),
Emval.toHandle(user_res.result.profile_photo_url || ''));
});
});
};
window.addEventListener('message', message_func);
Expand Down
2 changes: 1 addition & 1 deletion Source/Extensions/Dropbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Dropbox
{
bool HasToken();
/// @param aTokenCallback The name of a javascript accessible function which takes an Emval float containing the expiration time for the token.
/// @param aTokenCallback The name of a javascript accessible function which takes an Emval float containing the expiration time for the token, an Emval string of the user name and an Emval string with a URL to the profile picture.
void RequestToken(bool aShowPopup, std::string aTokenCallback);
void LogOut();

Expand Down
30 changes: 22 additions & 8 deletions Source/Extensions/GoogleDrive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ EM_JS(bool, gis_loaded, (), {
}
global_client_token = google.accounts.oauth2.initTokenClient({
client_id: '824603127976-vjf2sbqo99s9kulm1jp847c453ctmv65.apps.googleusercontent.com',
scope: 'https://www.googleapis.com/auth/drive',
scope: 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile',
prompt: '',
callback: '', // defined later
});
Expand All @@ -61,16 +61,16 @@ extern"C" EMSCRIPTEN_KEEPALIVE void GAPI_Init_Client()
EM_JS(void, init_gapi_with_key, (emscripten::EM_VAL APIKey), {
gapi.client.init({
apiKey: Emval.toValue(APIKey),
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest']
discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest', 'https://people.googleapis.com/$discovery/rest?version=v1']
}).then(()=>{global_gapi_inited = true;});
});

EM_JS(bool, has_gapi_token, (), {
if(gapi.client.getToken() !== null){
console.log('Already logged in');
}else{
console.log('Not logged in');
}
//if(gapi.client.getToken() !== null){
// console.log('Already logged in');
//}else{
// console.log('Not logged in');
//}
return gapi.client.getToken() !== null;
//return gapi.auth2.getAuthInstance().isSignedIn.get();
});
Expand All @@ -79,7 +79,21 @@ EM_JS(void, request_client_token, (emscripten::EM_VAL prompt, emscripten::EM_VAL
// Prompt the user to select a Google Account and ask for consent to share their data
// when establishing a new session.
var client_token_callback = Module[Emval.toValue(token_callback)];
global_client_token.callback = (token_data) =>{client_token_callback(Emval.toHandle(Date.now() + (token_data.expires_in * 1000)));};
global_client_token.callback = (token_data) => {
gapi.client.people.people.get({
"resourceName": "people/me",
"requestMask.includeField": "person.names,person.photos",
"sources": [
"READ_SOURCE_TYPE_PROFILE"
]
}).then((user_data)=>{
client_token_callback(
Emval.toHandle(Date.now() + (token_data.expires_in * 1000)),
Emval.toHandle(user_data.result.names.length != 0 ? user_data.result.names[0].displayName : '(Anon)'),
Emval.toHandle(user_data.result.photos.length != 0 ? user_data.result.photos[0].url : '')
);
});
};
global_client_token.requestAccessToken({prompt: Emval.toValue(prompt)});
//global_client_token.requestAccessToken();
});
Expand Down
2 changes: 1 addition & 1 deletion Source/Extensions/GoogleDrive.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GoogleDrive
{
bool Ready();
bool HasToken();
/// @param aTokenCallback The name of a javascript accessible function which takes an Emval float containing the expiration time for the token.
/// @param aTokenCallback The name of a javascript accessible function which takes an Emval float containing the expiration time for the token, an Emval string of the user name and an Emval string with a URL to the profile picture.
void RequestToken(bool aShowPopup, std::string aTokenCallback);
void LogOut();

Expand Down
89 changes: 38 additions & 51 deletions Source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ char* g_testStr = new char[50];
extern "C" EMSCRIPTEN_KEEPALIVE void ShowInputDebugger() { g_showInputDebugger = true; }
EM_JS(void, show_input_debugger, (), {_ShowInputDebugger(); });

std::string g_userName = "(Anon)";
bool g_hasGoogleAcc = false;
bool g_hasDropboxAcc = false;
bool g_fileTabOpenedThisFrame = true; // Only use in File tab!
Expand Down Expand Up @@ -79,29 +80,29 @@ extern "C" EMSCRIPTEN_KEEPALIVE void SaveProject()
ImGui::Ext::StopLoadingScreen();
}

extern "C" EMSCRIPTEN_KEEPALIVE void GoogleTokenExpirationCallback(emscripten::EM_VAL aTime)
extern "C" EMSCRIPTEN_KEEPALIVE void GoogleTokenExpirationCallback(emscripten::EM_VAL aTime, emscripten::EM_VAL aUserName, emscripten::EM_VAL aProfilePhotoURL)
{
Serialization::Preferences::SetDouble("Google/ExpirationDate", VAR_FROM_JS(aTime).as<double>());
Serialization::Preferences::SetBool("Google/IsLoggedIn", true);
g_userName = VAR_FROM_JS(aUserName).as<std::string>();
}
extern "C" EMSCRIPTEN_KEEPALIVE void LogInToGoogle()
{
float expiration = Serialization::Preferences::HasKey("Google/ExpirationDate") ? Serialization::Preferences::GetDouble("Google/ExpirationDate") : 0;
GoogleDrive::RequestToken(EM_ASM_DOUBLE({return Date.now();}) >= expiration, "_GoogleTokenExpirationCallback");
//GoogleDrive::RequestToken(!Serialization::Preferences::HasKey("Google/IsLoggedIn"));
g_closeFileTab = true;
}

extern "C" EMSCRIPTEN_KEEPALIVE void DropboxTokenExpirationCallback(emscripten::EM_VAL aTime)
extern "C" EMSCRIPTEN_KEEPALIVE void DropboxTokenExpirationCallback(emscripten::EM_VAL aTime, emscripten::EM_VAL aUserName, emscripten::EM_VAL aProfilePhotoURL)
{
Serialization::Preferences::SetDouble("Dropbox/ExpirationDate", VAR_FROM_JS(aTime).as<double>());
Serialization::Preferences::SetBool("Dropbox/IsLoggedIn", true);
g_userName = VAR_FROM_JS(aUserName).as<std::string>();
}
extern "C" EMSCRIPTEN_KEEPALIVE void LogInToDropbox()
{
float expiration = Serialization::Preferences::HasKey("Dropbox/ExpirationDate") ? Serialization::Preferences::GetDouble("Dropbox/ExpirationDate") : 0;
Dropbox::RequestToken(EM_ASM_DOUBLE({return Date.now();}) >= expiration, "_DropboxTokenExpirationCallback");
//Dropbox::RequestToken(!Serialization::Preferences::HasKey("Dropbox/IsLoggedIn"));
Dropbox::RequestToken(true || EM_ASM_DOUBLE({return Date.now();}) >= expiration, "_DropboxTokenExpirationCallback"); // TODO: fix token refresh
g_closeFileTab = true;
}
extern "C" EMSCRIPTEN_KEEPALIVE void OpenDropboxChooser()
Expand Down Expand Up @@ -181,82 +182,68 @@ void loop(void* window){
{
if(g_fileTabOpenedThisFrame)
{
g_hasGoogleAcc = GoogleDrive::HasToken() && EM_ASM_DOUBLE({return Date.now();}) >= Serialization::Preferences::GetDouble("Google/ExpirationDate");
g_hasGoogleAcc = GoogleDrive::HasToken() && EM_ASM_DOUBLE({return Date.now();}) <= Serialization::Preferences::GetDouble("Google/ExpirationDate");
g_hasDropboxAcc = Dropbox::HasToken() &&
//(!Serialization::Preferences::HasKey("Dropbox/ExpirationDate") ||
EM_ASM_DOUBLE({return Date.now();}) >= Serialization::Preferences::GetDouble("Dropbox/ExpirationDate");
EM_ASM_DOUBLE({return Date.now();}) <= Serialization::Preferences::GetDouble("Dropbox/ExpirationDate");
}
ImGui::MenuItem("Open Project", 0, false, !g_isSafeMode);
if(!g_isSafeMode){ImGui::Ext::CreateHTMLButton("OpenProject", "click", "_LoadProject");}
ImGui::MenuItem("Save Document");
ImGui::Ext::CreateHTMLButton("SaveProject", "click", "_SaveProject");
ImGui::Separator();
if(ImGui::BeginMenu("Google Drive", GoogleDrive::Ready()))
if(!g_hasGoogleAcc && !g_hasDropboxAcc)
{
if(!g_hasGoogleAcc)
{
if(ImGui::MenuItem("Log In With Google"))
{
//LogInToGoogle();
}
ImGui::Ext::CreateHTMLButton("GoogleLogin", "click", "_LogInToGoogle");
}
else
ImGui::MenuItem("Log In With Google");
ImGui::Ext::CreateHTMLButton("GoogleLogin", "click", "_LogInToGoogle");
ImGui::MenuItem("Log In to Dropbox");
ImGui::Ext::CreateHTMLButton("DropboxLogin", "click", "_LogInToDropbox");
ImGui::Separator();
ImGui::MenuItem("Open Project", 0, false, !g_isSafeMode);
if(!g_isSafeMode){ImGui::Ext::CreateHTMLButton("OpenProject", "click", "_LoadProject");}
ImGui::MenuItem("Save Document");
ImGui::Ext::CreateHTMLButton("SaveProject", "click", "_SaveProject");
}
else if(g_hasGoogleAcc)
{
ImGui::SeparatorText("Google Drive");
// Profile info + logout
ImGui::Text(g_userName.data());
if(ImGui::MenuItem("Log Out"))
{
// Profile info + logout
GoogleDrive::LogOut();
}
ImGui::Separator();
if(ImGui::MenuItem("Open Project", 0, false, !g_isSafeMode && g_hasGoogleAcc))
if(ImGui::MenuItem("Open Project", 0, false, !g_isSafeMode))
{
g_shouldDeleteOnLoad = true;
ImGui::Ext::StartLoadingScreen();
GoogleDrive::LoadProject("application/vnd.google-apps.folder", "_LoadFileFromCloudDrive", "_LoadCompletedFromCloudDrive", "_LoadCanceledFromCloudDrive");
}
if(ImGui::MenuItem("Save Document", 0, false, g_hasGoogleAcc && doc.GetFileID() != ""))
if(ImGui::MenuItem("Save Document", 0, false, doc.GetFileID() != ""))
{
ImGui::Ext::StartLoadingScreen();
GoogleDrive::SaveProject(doc.GetFileID(), doc.Save());
Serialization::KaraokeDocument::Get().UnsetIsDirty();
FileHandler::SyncLocalFS();
ImGui::Ext::StopLoadingScreen();
}
ImGui::EndMenu();
}
else
else if(g_hasDropboxAcc)
{
ImGui::Ext::DestroyHTMLElement("GoogleLogin");
}
if(ImGui::BeginMenu("Dropbox"))
{
if(!g_hasDropboxAcc)
{
if(ImGui::MenuItem("Log In to Dropbox"))
{
//LogInToDropbox();
}
ImGui::Ext::CreateHTMLButton("DropboxLogin", "click", "_LogInToDropbox");
}
else
ImGui::SeparatorText("Dropbox");
// Profile info + logout
ImGui::Text(g_userName.data());
if(ImGui::MenuItem("Log Out"))
{
// Profile info + logout
Dropbox::LogOut();
}
ImGui::Separator();
ImGui::MenuItem("Open Project", 0, false, !g_isSafeMode && g_hasDropboxAcc);
if(!g_isSafeMode && g_hasDropboxAcc) {ImGui::Ext::CreateHTMLButton("OpenDBProject", "click", "_OpenDropboxChooser");}
if(ImGui::MenuItem("Save Document", 0, false, g_hasDropboxAcc && doc.GetFileID() != ""))
ImGui::MenuItem("Open Project", 0, false, !g_isSafeMode);
if(!g_isSafeMode) {ImGui::Ext::CreateHTMLButton("OpenDBProject", "click", "_OpenDropboxChooser");}
if(ImGui::MenuItem("Save Document", 0, false, doc.GetFileID() != ""))
{
ImGui::Ext::StartLoadingScreen();
Dropbox::SaveProject(doc.GetFileID(), doc.Save());
Serialization::KaraokeDocument::Get().UnsetIsDirty();
FileHandler::SyncLocalFS();
ImGui::Ext::StopLoadingScreen();
}
ImGui::EndMenu();
}
else
{
ImGui::Ext::DestroyHTMLElement("DropboxLogin");
ImGui::Ext::DestroyHTMLElement("OpenDBProject");
}
ImGui::EndMenu();
g_fileTabOpenedThisFrame = false;
Expand Down Expand Up @@ -325,7 +312,7 @@ void loop(void* window){
}
ImGui::Separator();
ImGui::SeparatorText("Word Case");
if(ImGui::MenuItem("Majiscule", "EX,AM,PLE"))
if(ImGui::MenuItem("Majuscule", "EX,AM,PLE"))
{
int markedLine = TimingEditor::Get().GetMarkedLine();
// Find first token of word
Expand Down
Loading

0 comments on commit 269f84f

Please sign in to comment.