Skip to content

Commit

Permalink
FIXED pathnames in data files created on Win32
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown authored and unknown committed May 19, 2010
1 parent 33c6706 commit 1faf80c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 23 deletions.
24 changes: 10 additions & 14 deletions src/common/gui_file.cc
Expand Up @@ -28,6 +28,7 @@

#include <gtk/gtk.h>
#include "gui_file.h"
#include "util.h"

// create a new file selection window
GuiFile::GuiFile (GtkWindow *parent, GtkFileChooserAction action, const std::string &title, const std::string &file)
Expand Down Expand Up @@ -72,19 +73,6 @@ GuiFile::GuiFile (GtkWindow *parent, GtkFileChooserAction action, const std::str
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER(window), name);
g_free (name);
}

/*
FIXME: that uses code from dlgedit. Needs to be more generic.
// set shortcuts
const std::vector<std::string> & projects = CfgData::data->projectsFromDatadir ();
for (std::vector<std::string>::const_iterator i = projects.begin (); i != projects.end (); i++)
{
const std::string &dir = CfgData::data->getBasedir (*i);
if (dir.length () > 0)
gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER(window), dir.c_str (), NULL);
}
*/
}

// clean up
Expand All @@ -98,7 +86,7 @@ bool GuiFile::run ()
if (gtk_dialog_run (GTK_DIALOG (window)) == GTK_RESPONSE_ACCEPT)
{
gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (window));
File = filename;
File = MK_UNIX_PATH (filename);
g_free (filename);

return true;
Expand All @@ -107,6 +95,14 @@ bool GuiFile::run ()
return false;
}

void GuiFile::add_shortcut (const std::string & shortcut)
{
if (shortcut.length () > 0)
{
gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER(window), shortcut.c_str (), NULL);
}
}

void GuiFile::add_filter (const std::string & pattern, const std::string & name)
{
unsigned long pos;
Expand Down
6 changes: 6 additions & 0 deletions src/common/gui_file.h
Expand Up @@ -63,6 +63,12 @@ class GuiFile : public GuiModalDialog
*/
void add_filter (const std::string & pattern, const std::string & name);

/**
* Add shortcut to the given directory to the file chooser.
* @param shortcut the shortcut to add.
*/
void GuiFile::add_shortcut (const std::string & shortcut);

/**
* Return the users selection.
* @param the users selection.
Expand Down
21 changes: 16 additions & 5 deletions src/common/util.cc
Expand Up @@ -31,7 +31,7 @@
#define realpath(N,R) _fullpath((R),(N),_MAX_PATH)
#undef PATH_MAX
#define PATH_MAX _MAX_PATH
#endif WIN32
#endif

#include <base/base.h>

Expand All @@ -40,14 +40,14 @@
// get path relative to data directory
std::string util::get_relative_path (const std::string & path, const std::string & target_dir)
{
std::string base_path = base::Paths.user_data_dir();
std::string base_path = MK_UNIX_PATH (base::Paths.user_data_dir());
std::string rel_path = path;

// make sure to use path relative to (user defined) data directory
if (base_path == "" || !remove_common_path (rel_path, base_path))
{
// fallback to builtin data dir if that doesn't seem to work
base_path = base::Paths.game_data_dir();
base_path = MK_UNIX_PATH (base::Paths.game_data_dir());
if (!remove_common_path (rel_path, base_path))
{
// if everythin fails, try locating target_dir in the path and use
Expand All @@ -73,11 +73,11 @@ bool util::remove_common_path (std::string & path, const std::string & base_path
std::string c_base_path = canonical_path;
if (realpath(path.c_str(), canonical_path))
{
path = canonical_path;
path = MK_UNIX_PATH (canonical_path);
if (path.compare (0, c_base_path.size(), c_base_path) == 0)
{
path = path.substr (c_base_path.length());
if (path[0] == '/' || path[0] == '\\')
if (path[0] == '/')
{
path = path.substr (1);
}
Expand All @@ -88,3 +88,14 @@ bool util::remove_common_path (std::string & path, const std::string & base_path

return false;
}

// convert windows directory separators to unix style
std::string util::to_unix_path (const std::string & path)
{
std::string result = path;
for (std::string::iterator i = result.begin(); i != result.end(); i++)
{
if (*i == '\\') *i = '/';
}
return result;
}
14 changes: 14 additions & 0 deletions src/common/util.h
Expand Up @@ -29,6 +29,12 @@

#include <string>

#ifdef WIN32
#define MK_UNIX_PATH(path) util::to_unix_path(path)
#else
#define MK_UNIX_PATH(path) path
#endif

/**
* Some helper methods used across the different tools.
*/
Expand All @@ -46,6 +52,14 @@ class util
*/
static std::string get_relative_path (const std::string & path, const std::string & target_dir);

/**
* Convert windows directory names to unix directory names. All paths in
* data files are supposed to be in unix-style notation.
* @param path the pathname to convert.
* @return path where '\\' is replaced with '/'.
*/
static std::string to_unix_path (const std::string & path);

private:
/**
* Try to create a path relative to the given base path.
Expand Down
17 changes: 17 additions & 0 deletions src/dlgedit/gui_dlgedit_events.cc
Expand Up @@ -27,6 +27,7 @@
*/

#include <gtk/gtk.h>
#include "cfg_data.h"
#include "gui_dlgedit.h"
#include "gui_file.h"

Expand All @@ -53,6 +54,14 @@ void on_file_load_activate (GtkMenuItem * menuitem, gpointer user_data)
GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_OPEN, "Load dialogue source", dlgedit->directory ());
fs.add_filter ("*"FILE_EXT, "Adonthell Dialogue Source");

// set shortcuts
const std::vector<std::string> & projects = CfgData::data->projectsFromDatadir ();
for (std::vector<std::string>::const_iterator i = projects.begin (); i != projects.end (); i++)
{
const std::string &dir = CfgData::data->getBasedir (*i);
fs.add_shortcut (dir);
}

// File selection closed with OK
if (fs.run ()) dlgedit->loadDialogue (fs.getSelection ());
}
Expand All @@ -77,6 +86,14 @@ void on_file_save_as_activate (GtkMenuItem * menuitem, gpointer user_data)
GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_SAVE, "Save dialogue source", dlgedit->filename ());
fs.add_filter ("*"FILE_EXT, "Adonthell Dialogue Source");

// set shortcuts
const std::vector<std::string> & projects = CfgData::data->projectsFromDatadir ();
for (std::vector<std::string>::const_iterator i = projects.begin (); i != projects.end (); i++)
{
const std::string &dir = CfgData::data->getBasedir (*i);
fs.add_shortcut (dir);
}

// File selection closed with OK
if (fs.run ()) dlgedit->saveDialogue (fs.getSelection ());
}
Expand Down
10 changes: 9 additions & 1 deletion src/dlgedit/gui_graph.cc
Expand Up @@ -233,9 +233,17 @@ bool GuiGraph::newModule (DlgPoint &point)

// allow the user to select a module
GtkWindow *parent = GTK_WINDOW (GuiDlgedit::window->getWindow());
GuiFile fs = GuiFile (parent, GTK_FILE_CHOOSER_ACTION_OPEN, "Select sub-dialogue to add", dir + "/");
GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_OPEN, "Select sub-dialogue to add", dir + "/");
fs.add_filter ("*"FILE_EXT, "Adonthell Dialogue Source");

// set shortcuts
const std::vector<std::string> & projects = CfgData::data->projectsFromDatadir ();
for (std::vector<std::string>::const_iterator i = projects.begin (); i != projects.end (); i++)
{
const std::string &dir = CfgData::data->getBasedir (*i);
fs.add_shortcut (dir);
}

if (fs.run ())
{
DlgModule *subdlg = GuiDlgedit::window->loadSubdialogue (fs.getSelection());
Expand Down
2 changes: 1 addition & 1 deletion src/mapedit/gui_entity_list.cc
Expand Up @@ -443,7 +443,7 @@ void GuiEntityList::scanDir (const std::string & datadir, GtkListStore *model)
if (S_ISREG (statbuf.st_mode) && filepath.compare (filepath.length() - 4, 4, ".xml") == 0)
{
// check if this file is already part of the map
if (isPresentOnMap (filepath)) continue;
if (isPresentOnMap (MK_UNIX_PATH(filepath))) continue;

// not present on map, so add it to the list

Expand Down
4 changes: 4 additions & 0 deletions src/mapedit/gui_mapedit_events.cc
Expand Up @@ -29,6 +29,8 @@
#include <string>
#include <gtk/gtk.h>

#include "base/base.h"

#include "gui_mapedit.h"
#include "gui_mapview.h"
#include "gui_grid.h"
Expand Down Expand Up @@ -57,6 +59,7 @@ void on_file_load_activate (GtkMenuItem * menuitem, gpointer user_data)

GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_OPEN, "Load map", mapedit->directory ());
fs.add_filter ("*.xml", "Adonthell Map");
fs.add_shortcut (base::Paths.user_data_dir() + "/");

// File selection closed with OK
if (fs.run ()) mapedit->loadMap (fs.getSelection ());
Expand All @@ -83,6 +86,7 @@ void on_file_save_as_activate (GtkMenuItem * menuitem, gpointer user_data)

GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_SAVE, "Save Map", mapedit->filename ());
fs.add_filter ("*.xml", "Adonthell Map");
fs.add_shortcut (base::Paths.user_data_dir() + "/");

// File selection closed with OK
if (fs.run ()) mapedit->saveMap (fs.getSelection ());
Expand Down
7 changes: 5 additions & 2 deletions src/modeller/gui_modeller.cc
Expand Up @@ -77,6 +77,7 @@ static void on_file_load (GtkMenuItem * menuitem, gpointer user_data)
// open file chooser
GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_OPEN, "Open Model", directory + "/");
fs.add_filter ("*.xml", "Adonthell Model");
fs.add_shortcut (base::Paths.user_data_dir() + "/models/");

// File selection closed with OK
if (fs.run ()) modeller->loadModel (fs.getSelection ());
Expand Down Expand Up @@ -122,7 +123,8 @@ static void on_file_save_as_activate (GtkMenuItem * menuitem, gpointer user_data

GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_SAVE, "Save Model", saveDir + "/" + filename);
fs.add_filter ("*.xml", "Adonthell Model");

fs.add_shortcut (base::Paths.user_data_dir() + "/models/");

// File selection closed with OK
if (fs.run ()) modeller->saveModel (fs.getSelection ());
}
Expand Down Expand Up @@ -246,7 +248,8 @@ static void on_add_sprite_pressed (GtkButton * button, gpointer user_data)

GuiFile fs (parent, GTK_FILE_CHOOSER_ACTION_OPEN, "Load Sprite", modeller->spriteDirectory () + "/");
fs.add_filter ("*.xml|*.png", "Adonthell Sprite");

fs.add_shortcut (base::Paths.user_data_dir() + "/gfx/");

// File selection closed with OK
if (fs.run ()) modeller->addSprite (fs.getSelection ());
}
Expand Down

0 comments on commit 1faf80c

Please sign in to comment.