Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Move more things from cefclient_gtk to appshell_helpers #588

Merged
merged 1 commit into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions appshell/appshell_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <string>

#include "include/cef_command_line.h"
#include "include/internal/cef_string.h"

#include "config.h"
Expand All @@ -45,4 +46,15 @@ CefString AppGetProductVersionString();
// Returns a string containing "Chrome/" appends with its version (e.g. "Chrome/29.0.1547.65")
CefString AppGetChromiumVersionString();

#ifdef OS_LINUX

char* AppInitWorkingDirectory();
std::string AppGetWorkingDirectory();
std::string AppGetRunningDirectory();

int AppInitInitialURL(CefRefPtr<CefCommandLine> command_line);
std::string AppGetInitialURL();

#endif

} // namespace appshell
86 changes: 86 additions & 0 deletions appshell/appshell_helpers_gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "appshell/appshell_helpers.h"

#include "appshell/browser/resource.h"
#include "appshell/common/client_switches.h"
#include "include/cef_base.h"
#include "include/cef_version.h"

Expand All @@ -33,12 +34,17 @@
//#include <MMSystem.h>
//#include <ShlObj.h>
#include <glib.h>
#include <sys/stat.h>

extern time_t g_appStartupTime;
extern char _binary_appshell_appshell_extensions_js_start;

namespace appshell {

char szWorkingDir[512]; // The current working directory
std::string szRunningDir;
std::string szInitialUrl;

CefString GetCurrentLanguage()
{
const char* locconst = pango_language_to_string( gtk_get_default_language() );
Expand Down Expand Up @@ -117,4 +123,84 @@ CefString AppGetChromiumVersionString() {
return CefString("");
}

char* AppInitWorkingDirectory() {
return getcwd(szWorkingDir, sizeof (szWorkingDir));
}

std::string AppGetWorkingDirectory() {
return szWorkingDir;
}

std::string AppGetRunningDirectory() {
if(szRunningDir.length() > 0)
return szRunningDir;

char buf[512];
int len = readlink("/proc/self/exe", buf, 512);

if(len < 0)
return AppGetWorkingDirectory(); //# Well, can't think of any real-world case where this would be happen

for(; len >= 0; len--){
if(buf[len] == '/'){
buf[len] = '\0';
szRunningDir.append(buf);
return szRunningDir;
}
}
}

bool FileExists(std::string path) {
struct stat buf;
return (stat(path.c_str(), &buf) >= 0) && (S_ISREG(buf.st_mode));
}

int GetInitialUrl(std::string& url) {
GtkWidget *dialog;
const char* dialog_title = "Please select the index.html file";
GtkFileChooserAction file_or_directory = GTK_FILE_CHOOSER_ACTION_OPEN;
dialog = gtk_file_chooser_dialog_new(dialog_title,
NULL,
file_or_directory,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);

if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
url = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
gtk_widget_destroy (dialog);
return 0;
}

return -1;
}

int AppInitInitialURL(CefRefPtr<CefCommandLine> command_line) {
if (command_line->HasSwitch(client::switches::kStartupPath)) {
szInitialUrl = command_line->GetSwitchValue(client::switches::kStartupPath);
return 0;
}

std::string url = AppGetRunningDirectory();
url.append("/dev/src/index.html");

if (!FileExists(url)) {
url = AppGetRunningDirectory();
url.append("/www/index.html");

if (!FileExists(url)) {
if (GetInitialUrl(url) < 0) {
return -1;
}
}
}

szInitialUrl = url;
return 0;
}

std::string AppGetInitialURL() {
return szInitialUrl;
}

} // namespace appshell
72 changes: 4 additions & 68 deletions appshell/cefclient_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
#include "appshell_node_process.h"

static std::string APPICONS[] = {"appshell32.png","appshell48.png","appshell128.png","appshell256.png"};
char szWorkingDir[512]; // The current working directory
std::string szInitialUrl;
std::string szRunningDir;
int add_handler_id;
bool isReallyClosing = false;

Expand Down Expand Up @@ -62,56 +59,8 @@ static gboolean HandleQuit(int signatl) {
destroy();
}

bool FileExists(std::string path) {
struct stat buf;
return (stat(path.c_str(), &buf) >= 0) && (S_ISREG(buf.st_mode));
}

int GetInitialUrl() {
GtkWidget *dialog;
const char* dialog_title = "Please select the index.html file";
GtkFileChooserAction file_or_directory = GTK_FILE_CHOOSER_ACTION_OPEN ;
dialog = gtk_file_chooser_dialog_new (dialog_title,
NULL,
file_or_directory,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
NULL);

if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{
szInitialUrl = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
gtk_widget_destroy (dialog);
return 0;
}
return -1;
}

// Global functions

std::string AppGetWorkingDirectory() {
return szWorkingDir;
}

std::string AppGetRunningDirectory() {
if(szRunningDir.length() > 0)
return szRunningDir;

char buf[512];
int len = readlink("/proc/self/exe", buf, 512);

if(len < 0)
return AppGetWorkingDirectory(); //# Well, can't think of any real-world case where this would be happen

for(; len >= 0; len--){
if(buf[len] == '/'){
buf[len] = '\0';
szRunningDir.append(buf);
return szRunningDir;
}
}
}

GtkWidget* AddMenuEntry(GtkWidget* menu_widget, const char* text,
GCallback callback) {
GtkWidget* entry = gtk_menu_item_new_with_label(text);
Expand Down Expand Up @@ -147,7 +96,7 @@ int main(int argc, char* argv[]) {
return exit_code;

//Retrieve the current working directory
if (!getcwd(szWorkingDir, sizeof (szWorkingDir)))
if (!appshell::AppInitWorkingDirectory())
return -1;

GtkWidget* window;
Expand All @@ -168,21 +117,8 @@ int main(int argc, char* argv[]) {
CefString(&settings.cache_path) = appshell::AppGetCachePath();
}

if (cmdLine->HasSwitch(client::switches::kStartupPath)) {
szInitialUrl = cmdLine->GetSwitchValue(client::switches::kStartupPath);
} else {
szInitialUrl = AppGetRunningDirectory();
szInitialUrl.append("/dev/src/index.html");

if (!FileExists(szInitialUrl)) {
szInitialUrl = AppGetRunningDirectory();
szInitialUrl.append("/www/index.html");

if (!FileExists(szInitialUrl)) {
if (GetInitialUrl() < 0)
return 0;
}
}
if (appshell::AppInitInitialURL(cmdLine) < 0) {
return 0;
}

// Initialize CEF.
Expand Down Expand Up @@ -247,7 +183,7 @@ int main(int argc, char* argv[]) {
CefBrowserHost::CreateBrowser(
window_info,
static_cast<CefRefPtr<CefClient> >(g_handler),
"file://"+szInitialUrl, browserSettings, NULL);
"file://" + appshell::AppGetInitialURL(), browserSettings, NULL);

gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show_all(GTK_WIDGET(window));
Expand Down