Michael Schwartz
Michael Schwartz

Reputation: 8425

Grab File Path in C + WebkitGTK

I'm trying to compile my web app as a native desktop application in C. However I'm having a bit of trouble grabbing the file path in C.

In PyGTK I would use...

import webkit, pygtk, gtk, os

path=os.getcwd()
print path
web_view.open("file://" + path + "/index.html")

However I'm not sure if I'm just looking in the wrong places or what, but when I search Google I haven't been able to find out how to grab the file path in C which I want to use like this.

gchar* uri = (gchar*) (argc > 1 ? argv[1] : "file://" + path + "app/index.html");

Instead of linking to it in a grotesque manner like so...

gchar* uri = (gchar*) (argc > 1 ? argv[1] : "file://" + /home/michael/Desktop/kodeWeave/linux/app/index.html");
webkit_web_view_open (web_view, uri);

Here's my full project (if helpful).

#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <webkit/webkit.h>

static WebKitWebView* web_view;

void on_window_destroy (GtkObject *object, gpointer user_data) {
  gtk_main_quit();
}

int main (int argc, char *argv[]) {
  GtkBuilder *builder;
  GtkWidget  *window;
  GtkWidget  *scrolled_window;

  gtk_init(&argc, &argv);

  builder = gtk_builder_new();
  gtk_builder_add_from_file (builder, "browser.xml", NULL);
  window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
  scrolled_window = GTK_WIDGET (gtk_builder_get_object (builder, "scrolledwindow1"));
  g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);

  gtk_window_set_title(GTK_WINDOW(window), "kodeWeave");

  web_view = WEBKIT_WEB_VIEW (webkit_web_view_new());
  gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (web_view));

  gtk_builder_connect_signals (builder, NULL);

  g_object_unref (G_OBJECT (builder));

  gchar* uri = (gchar*) (argc > 1 ? argv[1] : "file:///home/michael/Desktop/kodeWeave/linux/app/index.html");
  webkit_web_view_open (web_view, uri);

  gtk_widget_grab_focus (GTK_WIDGET (web_view));
  gtk_widget_show_all (window);

  gtk_main();

  return 0;
}

Upvotes: 0

Views: 200

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You can't use the + operator to concatenate strings in c, you may need snprintf instead, first you need a large enough buffer, may be the constant PATH_MAX will work, it's defined in limits.h, so for example

char uri[PATH_MAX];
char cwd[PATH_MAX];

getcwd(cwd, sizeof(cwd));

if (argc > 1)
    snprintf(uri, sizeof(uri), "%s", argv[1]);
else
    snprintf(uri, sizeof(uri), "file://%s/index.html", cwd);
    /*                                  ^ %s specifier for ^ this char pointer */

the + operator works with your operands, but in a different way, it just performs pointer arithmetic, because the operands are pointers.

Upvotes: 1

Related Questions