Skip to content

Commit

Permalink
GeoDa shows Debug window after open data from a directory with specia…
Browse files Browse the repository at this point in the history
…l characters (Windows Only) #1782
  • Loading branch information
lixun910 committed Dec 19, 2018
1 parent 657eb17 commit fc80380
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 75 deletions.
65 changes: 34 additions & 31 deletions DialogTools/ConnectDatasourceDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <algorithm>
#include <string>
#include <fstream>
#include <iostream>
#include <wx/progdlg.h>
#include <wx/filedlg.h>
#include <wx/listctrl.h>
Expand Down Expand Up @@ -113,52 +114,48 @@ void RecentDatasource::Init(wxString json_str_)
return;

// "recent_ds" : [{"ds_name":"/data/test.shp", "layer_name":"test", "ds_config":"...", "thumb":"..."}, ]
std::string json_str(json_str_.mb_str());
json_spirit::Value v;
std::wstring json_str = json_str_.ToStdWstring();
json_spirit::wValue v;

try {
if (!json_spirit::read(json_str, v)) {
throw std::runtime_error("Could not parse recent ds string");
throw GdaException("Could not parse recent ds string");
}

const json_spirit::Array& ds_list = v.get_array();
const json_spirit::wArray& ds_list = v.get_array();

n_ds = ds_list.size();

for (size_t i=0; i<n_ds; i++) {
const json_spirit::Object& o = ds_list[i].get_obj();
const json_spirit::wObject& o = ds_list[i].get_obj();
wxString ds_name, ds_conf, layer_name, ds_thumb;

for (json_spirit::Object::const_iterator i=o.begin(); i!=o.end(); ++i)
for (json_spirit::wObject::const_iterator i=o.begin(); i!=o.end();
++i)
{
json_spirit::Value val;
if (i->name_ == "ds_name") {
json_spirit::wValue val;
if (i->name_ == L"ds_name") {
val = i->value_;
std::string tmp = val.get_str();
const char* t = tmp.c_str();
int n = strlen(t);
ds_name = wxString::FromUTF8(t, n);
ds_name = val.get_str();
}
else if (i->name_ == "layer_name") {
val = i->value_;
layer_name = wxString::FromUTF8(val.get_str().c_str());
layer_name = val.get_str();
}
else if (i->name_ == "ds_config") {
val = i->value_;
ds_conf = wxString::FromUTF8(val.get_str().c_str());
ds_conf = val.get_str();
}
else if (i->name_ == "ds_thumb") {
val = i->value_;
ds_thumb = wxString(val.get_str());
ds_thumb = val.get_str();
}
}
ds_names.push_back(ds_name);
ds_layernames.push_back(layer_name);
ds_confs.push_back(ds_conf);
ds_thumbnails.push_back(ds_thumb);
}


} catch (std::runtime_error e) {
wxString msg;
msg << "Get Latest DB infor: JSON parsing failed: ";
Expand All @@ -170,25 +167,31 @@ void RecentDatasource::Init(wxString json_str_)
void RecentDatasource::Save()
{
// update ds_json_str from ds_names & ds_values
json_spirit::Array ds_list_obj;
json_spirit::wArray ds_list_obj;

for (int i=0; i<n_ds; i++) {
json_spirit::Object ds_obj;
std::string ds_name( ds_names[i].mb_str(wxConvUTF8));
std::string layer_name( ds_layernames[i].mb_str(wxConvUTF8));
std::string ds_conf( ds_confs[i].mb_str(wxConvUTF8) );
std::string ds_thumb( ds_thumbnails[i].mb_str(wxConvUTF8) );
ds_obj.push_back( json_spirit::Pair("ds_name", ds_name) );
ds_obj.push_back( json_spirit::Pair("layer_name", layer_name) );
ds_obj.push_back( json_spirit::Pair("ds_config", ds_conf) );
ds_obj.push_back( json_spirit::Pair("ds_thumb", ds_thumb) );
json_spirit::wObject ds_obj;
//const char* ds_name = ds_names[i].mb_str(wxConvUTF8);
//const char* layer_name = ds_layernames[i].mb_str(wxConvUTF8);
//const char* ds_conf = ds_confs[i].mb_str(wxConvUTF8);
//const char* ds_thumb = ds_thumbnails[i].mb_str(wxConvUTF8);
std::wstring ds_name = ds_names[i].ToStdWstring();
std::wstring layer_name = ds_layernames[i].ToStdWstring();
std::wstring ds_conf = ds_confs[i].ToStdWstring();
std::wstring ds_thumb = ds_thumbnails[i].ToStdWstring();
//std::string ds_name( ds_names[i].mb_str(wxConvUTF8));
//std::string layer_name( ds_layernames[i].mb_str(wxConvUTF8));
//std::string ds_conf( ds_confs[i].mb_str(wxConvUTF8) );
//std::string ds_thumb( ds_thumbnails[i].mb_str(wxConvUTF8) );
ds_obj.push_back( json_spirit::wPair(L"ds_name", ds_name) );
ds_obj.push_back( json_spirit::wPair(L"layer_name", layer_name) );
ds_obj.push_back( json_spirit::wPair(L"ds_config", ds_conf) );
ds_obj.push_back( json_spirit::wPair(L"ds_thumb", ds_thumb) );
ds_list_obj.push_back( ds_obj);
}

std::string json_str = json_spirit::write(ds_list_obj);
ds_json_str = json_str;

OGRDataAdapter::GetInstance().AddEntry(KEY_NAME_IN_GDA_HISTORY, json_str);
ds_json_str = json_spirit::write(ds_list_obj);
OGRDataAdapter::GetInstance().AddEntry(KEY_NAME_IN_GDA_HISTORY, ds_json_str);
}

void RecentDatasource::Add(wxString ds_name, wxString ds_conf, wxString layer_name,
Expand Down
40 changes: 10 additions & 30 deletions DialogTools/ReportBugDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,48 +148,37 @@ size_t write_to_string_(void *ptr, size_t size, size_t count, void *stream) {
return size*count;
}

string CreateIssueOnGithub(string& post_data)
string CreateIssueOnGithub(wxString& post_data)
{
std::vector<wxString> tester_ids = OGRDataAdapter::GetInstance().GetHistory("tester_id");
if (tester_ids.empty()) {
return "";
}
if (tester_ids.empty()) return "";

wxString tester_id = tester_ids[0];

string url = "https://api.github.com/repos/GeoDaCenter/geoda/issues";

const char url[] = "https://api.github.com/repos/GeoDaCenter/geoda/issues";
wxString header_auth = "Authorization: token " + tester_id;

wxString header_user_agent = "User-Agent: GeoDaTester";

string response;

CURL* curl = curl_easy_init();
CURLcode res;
if (curl) {
struct curl_slist *chunk = NULL;

chunk = curl_slist_append(chunk, header_auth.c_str());
chunk = curl_slist_append(chunk, header_user_agent.c_str());

// set our custom set of headers
res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data.c_str());

curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,
(const char*)post_data.mb_str(wxConvUTF8));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string_);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L);

res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

/* free the custom headers */
curl_slist_free_all(chunk);

}

return response;
}

Expand Down Expand Up @@ -273,8 +262,6 @@ ReportBugDlg::ReportBugDlg(wxWindow* parent, wxWindowID id,
SetParent(parent);
SetPosition(pos);
Centre();


}

ReportBugDlg::~ReportBugDlg()
Expand Down Expand Up @@ -349,8 +336,7 @@ bool ReportBugDlg::CreateIssue(wxString title, wxString body)

wxTextFile tfile;
if (tfile.Open(logger_path)) {
while (!tfile.Eof())
{
while (!tfile.Eof()) {
body << tfile.GetNextLine() << "\\n";
}
}
Expand All @@ -370,14 +356,9 @@ bool ReportBugDlg::CreateIssue(wxString title, wxString body)
json_msg << "\", \"labels\": ";
json_msg << labels;
json_msg << "}";
//wxString json_msg = wxString::Format(msg_templ, title, body, labels);

string msg(json_msg.mb_str(wxConvUTF8));

string result = CreateIssueOnGithub(msg);

string result = CreateIssueOnGithub(json_msg);
// parse results

if (!result.empty()) {
json_spirit::Value v;
try {
Expand All @@ -392,8 +373,7 @@ bool ReportBugDlg::CreateIssue(wxString title, wxString body)
ReportResultDlg dlg(NULL, url_issue);
dlg.ShowModal();
return true;
}
catch (std::runtime_error e) {
} catch (std::runtime_error e) {
wxString msg;
msg << "JSON parsing failed: ";
msg << e.what();
Expand Down
20 changes: 12 additions & 8 deletions ShapeOperations/GdaCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const wxString GdaCache::DB_HOST_HIST = "db_host";
const wxString GdaCache::DB_PORT_HIST = "db_port";
const wxString GdaCache::DB_NAME_HIST = "db_name";
const wxString GdaCache::DB_UNAME_HIST = "db_uname";
const wxString GdaCache::WS_URL_HIST = "ws_url";
const wxString GdaCache::WS_URL_HIST = "ws_url";

wxString GdaCache::GetFullPath()
{
Expand Down Expand Up @@ -53,14 +53,16 @@ GdaCache::GdaCache()

history_table->ReadData();

for ( int i=0; i< history_table->n_rows; i++){
for ( int i=0; i< history_table->n_rows; i++) {
// all strings are encoded and stored using UTF8, so use
// wxString::FromUTF8 to read from char*
wxString key = history_table->GetValueAt(i, 0);
wxString val = history_table->GetValueAt(i, 1);
history_keys.push_back(key);
history_vals.push_back(val);
}
}catch(GdaException& e) {
//XXX
} catch(GdaException& e) {
throw GdaException("Construct GdaCache Failed.");
}
}

Expand Down Expand Up @@ -98,8 +100,9 @@ void GdaCache::AddHistory(wxString param_key, wxString param_val)
history_keys.push_back( param_key );
history_vals.push_back( param_val );
// add to spatialite table
wxString sql = "INSERT INTO history VALUES('"
wxString _sql = "INSERT INTO history VALUES('"
+ param_key +"','"+param_val + "')";
const char * sql = (const char*) _sql.mb_str(wxConvUTF8);
cach_ds_proxy->ExecuteSQL(sql);
}

Expand All @@ -109,7 +112,8 @@ void GdaCache::AddEntry(wxString param_key, wxString param_val)
if ( param_key == history_keys[i] ){
// update existing Entry
history_vals[i] = param_val;
wxString sql = "UPDATE history SET param_val='" + param_val +"' WHERE param_key='" + param_key + "'";
wxString _sql = "UPDATE history SET param_val='" + param_val +"' WHERE param_key='" + param_key + "'";
const char * sql = (const char*) _sql.mb_str(wxConvUTF8);
cach_ds_proxy->ExecuteSQL(sql);
return;
}
Expand All @@ -118,8 +122,8 @@ void GdaCache::AddEntry(wxString param_key, wxString param_val)
history_keys.push_back( param_key );
history_vals.push_back( param_val );
// add to spatialite table
wxString sql = "INSERT INTO history VALUES('" + param_key +"','"+param_val + "')";
//cach_ds_proxy->ExecuteSQL(sql);
wxString _sql = "INSERT INTO history VALUES('" + param_key +"','"+param_val + "')";
const char * sql = (const char*) _sql.mb_str(wxConvUTF8);
OGRLayer* tmp_layer = cach_ds_proxy->ds->ExecuteSQL(sql, 0, "SQLITE");
cach_ds_proxy->ds->ReleaseResultSet(tmp_layer);
}
Expand Down
15 changes: 10 additions & 5 deletions ShapeOperations/OGRDatasourceProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ OGRDatasourceProxy::OGRDatasourceProxy(wxString _ds_name, GdaConst::DataSourceTy
const char* pszDsPath = GET_ENCODED_FILENAME(ds_name);

wxString msg;
msg << _("Failed to open data source. Please check the data/datasource and check if the data type/format is supported by GeoDa.\n\nTip: you can set up the necessary GeoDa driver by following the instructions at:\n http://geodacenter.github.io/formats.html");
msg << _("Failed to open data source. Please check the data/datasource and "
"check if the data type/format is supported by GeoDa.\n\nTip: you "
"can set up the necessary GeoDa driver by following the "
"instructions at:\n http://geodacenter.github.io/formats.html");

if (ds_type == GdaConst::ds_unknown) {
throw GdaException(GET_ENCODED_FILENAME(msg));
Expand Down Expand Up @@ -226,20 +229,22 @@ vector<wxString> OGRDatasourceProxy::GetLayerNames()
return this->layer_names;
}

OGRLayerProxy* OGRDatasourceProxy::ExecuteSQL(wxString sql)
OGRLayerProxy* OGRDatasourceProxy::ExecuteSQL(wxString _sql)
{
const char * sql = (const char*) _sql.mb_str(wxConvUTF8);
OGRLayer* tmp_layer = ds->ExecuteSQL(sql, 0, 0);
//tmp_layer->SyncToDisk();
ds->ReleaseResultSet(tmp_layer);
return NULL;
}

OGRLayerProxy* OGRDatasourceProxy::GetLayerProxyBySQL(wxString sql)
OGRLayerProxy* OGRDatasourceProxy::GetLayerProxyBySQL(wxString _sql)
{
// Note: layer is not managed here. Memory leak is possible.
OGRLayer* layer = ds->ExecuteSQL(sql.c_str(), 0, 0);
const char * sql = (const char*) _sql.mb_str(wxConvUTF8);
OGRLayer* layer = ds->ExecuteSQL(sql, 0, 0);
if (layer == NULL) return NULL;
OGRLayerProxy* layer_proxy = new OGRLayerProxy(sql, layer, ds_type);
OGRLayerProxy* layer_proxy = new OGRLayerProxy(_sql, layer, ds_type);
return layer_proxy;
}

Expand Down
4 changes: 3 additions & 1 deletion ShapeOperations/OGRLayerProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ wxString OGRLayerProxy::GetValueAt(int rid, int cid, wxCSConv* m_wx_encoding)
{
wxString rst;
if (m_wx_encoding == NULL) {
rst = data[rid]->GetFieldAsString(cid);
// following GDAL/OGR using UTF8 to read table data,
// if no custom encoding specified
rst = wxString(data[rid]->GetFieldAsString(cid), wxConvUTF8);
} else {
rst = wxString(data[rid]->GetFieldAsString(cid), *m_wx_encoding);
}
Expand Down

0 comments on commit fc80380

Please sign in to comment.