void OpenGLExampleX11::renderThread()
{
    GdkGLContext *glcontext = gtk_widget_get_gl_context(drawing_area);
    try {
        while (!boost::this_thread::interruption_requested()) {
            gdk_threads_enter();
            GdkGLDrawable *gld = gtk_widget_get_gl_drawable (drawing_area);
            gdk_gl_drawable_make_current(gld, glcontext);
            gboolean tmp = gdk_gl_drawable_gl_begin(gld, glcontext);
            FBLOG_WARN("", "Setting current context to widget. glbegin was " << tmp);
            //render();
            gdk_gl_drawable_gl_end(gld);
            //FBLOG_INFO("", "Got drawable: " << gld);
            //if (gdk_gl_drawable_is_double_buffered (gld)) {
                //gdk_gl_drawable_swap_buffers (gld);
            //} else {
                //glFlush();
            //}
            gdk_flush();
            gdk_threads_leave();
            boost::this_thread::sleep(boost::posix_time::milliseconds(200));
        }
    } catch (const boost::thread_interrupted&) {
    }
    FBLOG_WARN("", "Thread stop signaled");
    FBLOG_WARN("", "Thread stopping");
}
bool OpenGLExampleX11::onWindowDetached(FB::DetachedEvent *evt, FB::PluginWindowX11 *win)
{
    FBLOG_WARN("", "DetachedEvent start");
    rThread.interrupt();
    rThread.join();
    // Shut down the opengl config and context objects
    if (glConfig) {
        g_object_unref(G_OBJECT(glConfig));
        glConfig = NULL;
    }
    GdkGLContext *glContext = gtk_widget_get_gl_context(drawing_area);
    if (glContext) {
        g_object_unref(G_OBJECT(glContext));
        glContext = NULL;
    }
    FBLOG_WARN("", "DetachedEvent end");
}
void ConfigManager::initConfig() {
    try {
        configMap = loadConfigMap(configFilePath);
    } catch (...) {
        // if we can't parse the document, throw the values out
        FBLOG_WARN("ConfigManager", "Could not read document from " << configFilePath);
        initConfigFile();
    }
}
void WebEmberRunner::quitEmber()
{
	mLinker.callQuitEmber();

	//temporary fix
	while(sRunning){
		try {
			boost::this_thread::sleep(boost::posix_time::milliseconds(1));
		} catch (...) {
			FBLOG_WARN("WebEmberRunner::quitEmber", "Ember wait loop failed with exception.");
		}
	}
}
bool OpenGLExampleX11::onWindowAttached(FB::AttachedEvent *evt, FB::PluginWindowX11 *win)
{
    FBLOG_WARN("", "AttachedEvent start");
    // Initialize GTK/GDK on this thread

    //boost::scoped_array<char> argv(new char[1][20]);
    int argc = 0;
    //strcpy(argv[0], "--sync");
    //gboolean initSucceeded = gtk_init_check(&argc, argv.get());

    gdk_threads_enter();
    FBLOG_WARN("", "Starting Render Thread");
    //gboolean initSucceeded = gdk_gl_init_check(&argc, NULL);
    //FBLOG_WARN("", "gdk_gl_init_check returned " << initSucceeded);

    drawing_area = gtk_drawing_area_new();
    gtk_widget_set_size_request(drawing_area, 500, 500);

    GdkGLConfigMode glconf = static_cast<GdkGLConfigMode>(
            GDK_GL_MODE_RGB |
            GDK_GL_MODE_DOUBLE |
            GDK_GL_MODE_DEPTH
            );
    //FBLOG_WARN("", "Trying to config");
    //glConfig = gdk_gl_config_new_by_mode(glconf);
    //FBLOG_WARN("", "Created config: " << glConfig);
    //if (!glConfig) {
        //FBLOG_WARN("", "Double-buffer didn't work. Trying single.");
        glconf = static_cast<GdkGLConfigMode>(
                GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH
                );
        glConfig = gdk_gl_config_new_by_mode(glconf);
        FBLOG_WARN("", "Created config: " << glConfig);
        if (!glConfig) {
            FBLOG_WARN("", "Couldn't initialize opengl. No idea why, sorry!");
            return false;
        } else {
            FBLOG_INFO("", "Single-buffer opengl");
        }
    //} else {
        //FBLOG_INFO("", "Double-buffer opengl");
    //}

    FBLOG_WARN("", "Setting GL capability on Widget: " << drawing_area);
    gtk_widget_set_gl_capability (drawing_area,
            glConfig,
            NULL,
            TRUE,
            GDK_GL_RGBA_TYPE);

    gtk_container_add(win->getGtkContainer(), drawing_area);
    gtk_widget_show(drawing_area);

    FBLOG_WARN("", "Getting GL Context");
    GdkGLContext *glcontext = gtk_widget_get_gl_context (drawing_area);
    FBLOG_WARN("", "Got GL Context: " << glcontext << " Now getting Drawable:");
    GdkGLDrawable *gldrawable = gtk_widget_get_gl_drawable (drawing_area);
    FBLOG_WARN("", "Got GL Drawable: " << gldrawable);

    // Attach our OpenGL context to the widget
    gdk_threads_leave();
    rThread = boost::thread(&OpenGLExampleX11::renderThread, this);
    FBLOG_WARN("", "AttachedEvent end");
}
void FileManagerAPI::initializePaths() {
	mProtocols.clear();
	/*
	 * Defines paths
	 */
	
	// Internal
	boost::filesystem::path internalPath = FB::BrowserPlugin::getFSPath();
	internalPath = internalPath.parent_path();
	internalPath /= std::string(PLUGIN_SHAREDIR);
	
	// Temp
	boost::filesystem::path tempPath = boost::filesystem::path(FB::System::getTempPath());
	tempPath /= FBSTRING_ProductDomain;
	tempPath /= boost::filesystem::unique_path();
	
	// Local
	boost::filesystem::path localPath = tempPath; // fallback
	FB::PluginCorePtr plugin = getFactory()->getPlugin();
	FB::URI loc = FB::URI::fromString(plugin->getHost()->getDOMWindow()->getLocation());
	if(!loc.domain.empty()) {
		std::string localPathString = FB::System::getLocalAppDataPath(FBSTRING_ProductDomain);
		if(!localPathString.empty()) {
			localPath = boost::filesystem::path(localPathString);
			localPath /= std::string(loc.domain);
		} else {
			FBLOG_WARN("FileManagerAPI::setFactory", "Can't find local path: using temp path");
		}
	} else {
		FBLOG_WARN("FileManagerAPI::setFactory", "Can't find browser url: using temp path");
	}
	
	// Create directories
	// boost::filesystem::create_directories(internalPath);
	boost::filesystem::create_directories(tempPath);
	boost::filesystem::create_directories(localPath);

	// Normalize
	internalPath = boost::filesystem::canonical(internalPath);
	tempPath = boost::filesystem::canonical(tempPath);
	localPath = boost::filesystem::canonical(localPath);

	// Make generics
	internalPath = boost::filesystem::path(internalPath.generic_string());
	tempPath = boost::filesystem::path(tempPath.generic_string());
	localPath = boost::filesystem::path(localPath.generic_string());

	
	mProtocols.push_back(Protocol(Protocol::internal, internalPath));
	mProtocols.push_back(Protocol(Protocol::temp, tempPath));
	mProtocols.push_back(Protocol(Protocol::local, localPath));
	
	
	// Order bigger path first
	mProtocols.sort(boost::bind(std::greater<std::size_t>(),
								boost::bind(&std::string::length, boost::bind(&boost::filesystem::path::string<std::string>, boost::bind(&Protocol::getPath, _1))),
								boost::bind(&std::string::length, boost::bind(&boost::filesystem::path::string<std::string>, boost::bind(&Protocol::getPath, _2)))
								)
								
	);

	
	for(std::list<Protocol>::iterator it = mProtocols.begin(); it != mProtocols.end(); ++it) {
		FBLOG_DEBUG("FileManagerAPI::setFactory", it->getProtocol() << "=" << it->getPath());
	}
}