| 1 |
#include "mnID.h" |
| 2 |
#include "mnDef.h" |
| 3 |
#include "mnNotePanelController.h" |
| 4 |
#include <wx/config.h> |
| 5 |
#include <wx/tokenzr.h> |
| 6 |
#include <wx/utils.h> |
| 7 |
#include <wx/xrc/xmlres.h> |
| 8 |
|
| 9 |
BEGIN_EVENT_TABLE(mnNotePanelController, wxEvtHandler) |
| 10 |
EVT_TEXT_ENTER(XRCID("searchTextCtrl"), mnNotePanelController::handleSearch) |
| 11 |
/* |
| 12 |
EVT_TREE_SEL_CHANGED(XRCID("searchTree"), mnNotePanelController::handleTreeItemSelect) |
| 13 |
EVT_TREE_SEL_CHANGING(XRCID("searchTree"), mnNotePanelController::handleTreeItemSelecting) |
| 14 |
EVT_TREE_BEGIN_LABEL_EDIT(XRCID("searchTree"), mnNotePanelController::handleBeginLabelEdit) |
| 15 |
EVT_TREE_END_LABEL_EDIT(XRCID("searchTree"), mnNotePanelController::handleEndLabelEdit) |
| 16 |
EVT_CLOSE(mnNotePanelController::handleClose) |
| 17 |
EVT_COMMAND(XRCID("wikiHtmlTextCtrl"), mnEVT_LINK_CLICK, mnNotePanelController::handleLinkClick) |
| 18 |
*/ |
| 19 |
END_EVENT_TABLE() |
| 20 |
|
| 21 |
mnNotePanelController::mnNotePanelController(mnNotePanel* inPanel, wxString* inWikiDir) |
| 22 |
{ |
| 23 |
wikiDir = new wxString(*inWikiDir); |
| 24 |
wiki = new mnModel(wikiDir->mb_str()); |
| 25 |
panel = inPanel; |
| 26 |
} |
| 27 |
|
| 28 |
#if 0 |
| 29 |
void mnNotePanelController::handleConfig(wxCommandEvent& event) |
| 30 |
{ |
| 31 |
wxDirDialog* dlg; |
| 32 |
wxString gotDirName; |
| 33 |
wxString* dirName = new wxString(); |
| 34 |
|
| 35 |
wxConfig* config = new wxConfig(wxT(CONF_APP_NAME)); |
| 36 |
|
| 37 |
if(config->Read(wxT(CONF_WIKI_DIR), dirName)) { |
| 38 |
if(wxMessageBox(*dirName, wxT("Change data directory?"), wxYES_NO|wxICON_QUESTION) == wxNO){ |
| 39 |
delete config; |
| 40 |
return; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
dlg = new wxDirDialog(frame); |
| 45 |
if(dlg->ShowModal() == wxID_OK) { |
| 46 |
gotDirName = dlg->GetPath(); |
| 47 |
delete wiki; |
| 48 |
wiki = new mnModel(gotDirName.mb_str()); |
| 49 |
config->Write(wxT(CONF_WIKI_DIR), gotDirName.c_str()); |
| 50 |
} |
| 51 |
delete config; |
| 52 |
|
| 53 |
frame->clearAllTree(); |
| 54 |
} |
| 55 |
#endif |
| 56 |
|
| 57 |
void mnNotePanelController::handleSearch(wxCommandEvent& event) |
| 58 |
{ |
| 59 |
wxString* str = new wxString(event.GetString()); |
| 60 |
WikiList* wikiList; |
| 61 |
WikiList::Node* node; |
| 62 |
|
| 63 |
wikiList = wiki->search(str->mb_str()); |
| 64 |
|
| 65 |
node = wikiList->GetFirst(); |
| 66 |
if(!node) { |
| 67 |
wxLogMessage(wxT("Not Found! [%s]"), (const char*)str->c_str()); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if(wiki->addSearchStr(str)) { |
| 72 |
wiki->addSearchList(str, wikiList); |
| 73 |
panel->addSearchResult(wiki, str); |
| 74 |
} |
| 75 |
else { /* str is already in search list */ |
| 76 |
delete wikiList; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
#if 0 |
| 81 |
void mnNotePanelController::handleGroup(wxCommandEvent& event) |
| 82 |
{ |
| 83 |
WikiList* wikiList; |
| 84 |
const wxArrayString* searchStrList; |
| 85 |
int i; |
| 86 |
wxCursor normal; |
| 87 |
wxCursor watch(wxCURSOR_WATCH); |
| 88 |
|
| 89 |
frame->SetCursor(watch); |
| 90 |
wiki->group(); |
| 91 |
frame->clearAllTree(); |
| 92 |
searchStrList = wiki->getSearchStrList(); |
| 93 |
for(i = 0; i < searchStrList->Count(); i++) { |
| 94 |
wxString& searchStr = searchStrList->Item(i); |
| 95 |
frame->addSearchResult(wiki, &searchStr); |
| 96 |
} |
| 97 |
frame->SetCursor(normal); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
void mnNotePanelController::handleNewButton(wxCommandEvent& event) |
| 102 |
{ |
| 103 |
int ans; |
| 104 |
WikiData* wikiData = wiki->newWikiData(); |
| 105 |
wxString plainTextStr; |
| 106 |
wxString newSubjectStr; |
| 107 |
mnWikiDataEntryDialog* dlg = new mnWikiDataEntryDialog(frame); |
| 108 |
const wxString* wikiText = wikiData->getText(); |
| 109 |
const wxString* subject = wikiData->getSubject(); |
| 110 |
|
| 111 |
dlg->setSubject(*subject); |
| 112 |
dlg->setPlainText(*wikiText); |
| 113 |
ans = dlg->ShowModal(); |
| 114 |
|
| 115 |
if(ans != wxID_OK) return ; |
| 116 |
|
| 117 |
plainTextStr = dlg->getPlainText(); |
| 118 |
newSubjectStr = dlg->getSubject(); |
| 119 |
wikiData->modText(&plainTextStr); |
| 120 |
wikiData->save(); |
| 121 |
|
| 122 |
wikiData->modSubject(&newSubjectStr); |
| 123 |
|
| 124 |
notifyUpdate(wikiData); |
| 125 |
|
| 126 |
delete dlg; |
| 127 |
} |
| 128 |
|
| 129 |
void mnNotePanelController::handleRemoveButton(wxCommandEvent& event) |
| 130 |
{ |
| 131 |
wxTreeItemData* item = frame->getSelectedItem(); |
| 132 |
|
| 133 |
if(frame->isEditableTreeLabel(frame->getSelection())) { |
| 134 |
if(wxMessageBox(wxT("Do you realy remove this memo?\n") + frame->getSelectedLabel(), wxT("Realy?"), wxYES_NO|wxICON_QUESTION) == wxYES){ |
| 135 |
((WikiData*)(item))->removeDataFile(); |
| 136 |
notifyUpdate((WikiData*)(item)); |
| 137 |
} |
| 138 |
else { |
| 139 |
return; |
| 140 |
} |
| 141 |
} |
| 142 |
else { |
| 143 |
wiki->removeSearchStr(frame->getSelectedLabel()); |
| 144 |
frame->removeSelectedItem(); |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
void mnNotePanelController::handleHighlightButton(wxCommandEvent& event) |
| 150 |
{ |
| 151 |
frame->highlightSelectedItem(); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
void mnNotePanelController::handleTreeItemSelect(wxTreeEvent& event) |
| 156 |
{ |
| 157 |
frame->showSelectedItemText(event); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
void mnNotePanelController::handleTreeItemSelecting(wxTreeEvent& event) |
| 162 |
{ |
| 163 |
} |
| 164 |
|
| 165 |
void mnNotePanelController::handleBeginLabelEdit(wxTreeEvent& event) |
| 166 |
{ |
| 167 |
if(!frame->isEditableTreeLabel(event.GetItem())) event.Veto(); |
| 168 |
} |
| 169 |
|
| 170 |
void mnNotePanelController::handleEndLabelEdit(wxTreeEvent& event) |
| 171 |
{ |
| 172 |
wxString newSubject = event.GetLabel(); |
| 173 |
wxTreeItemData* item = frame->getSelectedItem(); |
| 174 |
|
| 175 |
if(item == NULL) { |
| 176 |
return ; |
| 177 |
} |
| 178 |
|
| 179 |
if(newSubject.Len() == 0) { |
| 180 |
event.Veto(); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
((WikiData*)(item))->modSubject(&newSubject); |
| 185 |
|
| 186 |
event.Veto(); |
| 187 |
|
| 188 |
notifyUpdate((WikiData*)item); |
| 189 |
} |
| 190 |
|
| 191 |
void mnNotePanelController::handleClose(wxCloseEvent& event) |
| 192 |
{ |
| 193 |
frame->Destroy(); |
| 194 |
} |
| 195 |
|
| 196 |
void mnNotePanelController::handleExit(wxCommandEvent& event) |
| 197 |
{ |
| 198 |
frame->Close(); |
| 199 |
} |
| 200 |
|
| 201 |
void mnNotePanelController::handleTreeExpand(wxCommandEvent& event) |
| 202 |
{ |
| 203 |
frame->expandAllTree(); |
| 204 |
} |
| 205 |
|
| 206 |
void mnNotePanelController::handleTreeCollapse(wxCommandEvent& event) |
| 207 |
{ |
| 208 |
frame->collapseAllTree(); |
| 209 |
} |
| 210 |
|
| 211 |
void mnNotePanelController::handleTreeClear(wxCommandEvent& event) |
| 212 |
{ |
| 213 |
wiki->clearSearchStrList(); |
| 214 |
wiki->clearSearchResultList(); |
| 215 |
frame->clearAllTree(); |
| 216 |
} |
| 217 |
|
| 218 |
void mnNotePanelController::handleAbout(wxCommandEvent& event) |
| 219 |
{ |
| 220 |
wxMessageBox(wxT(MN_APP_VERSION), wxT("Abount MalonNote"), wxOK|wxICON_INFORMATION); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
void mnNotePanelController::handleEditButton(wxCommandEvent& event) |
| 225 |
{ |
| 226 |
wxString plainTextStr; |
| 227 |
wxString newSubjectStr; |
| 228 |
wxTreeItemData* itemData = frame->getSelectedItem(); |
| 229 |
wxString subject = frame->getSelectedLabel(); |
| 230 |
mnWikiDataEntryDialog* dlg = new mnWikiDataEntryDialog(frame); |
| 231 |
int ans; |
| 232 |
|
| 233 |
if(!frame->isEditableTreeLabel(frame->getSelection())) return; /* selection is not memo data */ |
| 234 |
|
| 235 |
const wxString* wikiText = ((WikiData*)(itemData))->getText(); |
| 236 |
|
| 237 |
dlg->setSubject(subject); |
| 238 |
dlg->setPlainText(*wikiText); |
| 239 |
ans = dlg->ShowModal(); |
| 240 |
|
| 241 |
if(ans != wxID_OK) return ; |
| 242 |
if(itemData == NULL) return ; |
| 243 |
|
| 244 |
plainTextStr = dlg->getPlainText(); |
| 245 |
newSubjectStr = dlg->getSubject(); |
| 246 |
((WikiData*)(itemData))->modText(&plainTextStr); |
| 247 |
((WikiData*)(itemData))->save(); |
| 248 |
|
| 249 |
((WikiData*)(itemData))->modSubject(&newSubjectStr); |
| 250 |
|
| 251 |
notifyUpdate((WikiData*)itemData); |
| 252 |
|
| 253 |
delete dlg; |
| 254 |
} |
| 255 |
|
| 256 |
void mnNotePanelController::notifyUpdate(WikiData* data) |
| 257 |
{ |
| 258 |
const wxArrayString* searchStrList= wiki->getSearchStrList(); |
| 259 |
char* tokenList[32]; |
| 260 |
|
| 261 |
memset(tokenList, 0, sizeof(char*)*32); |
| 262 |
for(int i = 0; i < searchStrList->Count(); i++) { |
| 263 |
wxString& searchStr = searchStrList->Item(i); |
| 264 |
if( wiki->makeSearchToken(searchStr.mb_str(), tokenList) ) { /* create token */ |
| 265 |
if(wiki->matchWithToken((wxString*)(data->getFileName()), tokenList)) { /* match */ |
| 266 |
wiki->addSearchResultList(&searchStr, data); |
| 267 |
frame->updateSearchResult(wiki, &searchStr); |
| 268 |
} |
| 269 |
else { /* not match */ |
| 270 |
if( wiki->delSearchResultList(&searchStr, data) ) { |
| 271 |
frame->updateSearchResult(wiki, &searchStr); /* only when suceed to delete */ |
| 272 |
} |
| 273 |
} |
| 274 |
for(int j = 0; tokenList[j] != NULL; j++) free(tokenList[j]); |
| 275 |
memset(tokenList, 0, sizeof(char*)*32); |
| 276 |
} |
| 277 |
} |
| 278 |
data->setOldSubjectFromCurrent(); // restore old subject |
| 279 |
} |
| 280 |
|
| 281 |
void mnNotePanelController::handleLinkClick(wxCommandEvent &event) |
| 282 |
{ |
| 283 |
const wxString* wikiDataDir; |
| 284 |
wxString href = event.GetString(); |
| 285 |
wxString tag; |
| 286 |
wxString val; |
| 287 |
wxString doc; |
| 288 |
wxStringTokenizer tkz(href, wxT(":")); |
| 289 |
|
| 290 |
tag = tkz.GetNextToken(); |
| 291 |
val = tkz.GetNextToken(); |
| 292 |
if(tag.CmpNoCase(wxT("mnlink")) == 0) { |
| 293 |
event.SetString(val); |
| 294 |
handleSearch(event); |
| 295 |
} |
| 296 |
else if(tag.CmpNoCase(wxT("mndoc")) == 0) { |
| 297 |
wikiDataDir = wiki->getWikiDataDir(); |
| 298 |
doc = *wikiDataDir + wxT("/docs/") + val; |
| 299 |
openDoc(doc); |
| 300 |
} |
| 301 |
else { |
| 302 |
openUrl(href); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
void mnNotePanelController::handleOpenDocsDir(wxCommandEvent &event) |
| 307 |
{ |
| 308 |
const wxString* wikiDataDir; |
| 309 |
wxString cmd; |
| 310 |
wxString docDir; |
| 311 |
|
| 312 |
wikiDataDir = wiki->getWikiDataDir(); |
| 313 |
docDir = *wikiDataDir + wxT("/docs/"); |
| 314 |
|
| 315 |
/* create docs directory, anyway */ |
| 316 |
cmd.sprintf(wxT("mkdir "), docDir.c_str()); |
| 317 |
wxExecute(cmd); |
| 318 |
|
| 319 |
/* open docs directory */ |
| 320 |
openDoc(docDir); |
| 321 |
} |
| 322 |
|
| 323 |
void mnNotePanelController::openUrl(wxString& url) |
| 324 |
{ |
| 325 |
wxString cmd; |
| 326 |
|
| 327 |
#ifdef __WXMSW__ |
| 328 |
cmd.sprintf(wxT("cmd /c \"start %s\""), url.c_str()); |
| 329 |
#endif |
| 330 |
|
| 331 |
#ifdef __WXMAC__ |
| 332 |
cmd.sprintf(wxT("open %s"), url.c_str()); |
| 333 |
#endif |
| 334 |
|
| 335 |
#ifdef __WXCOCOA__ |
| 336 |
cmd.sprintf(wxT("open %s"), url.c_str()); |
| 337 |
#endif |
| 338 |
|
| 339 |
#ifdef __WXGTK__ |
| 340 |
cmd.sprintf(wxT("gnome-open %s"), url.c_str()); |
| 341 |
#endif |
| 342 |
|
| 343 |
//wxExecute(argv); |
| 344 |
wxExecute(cmd); |
| 345 |
} |
| 346 |
|
| 347 |
void mnNotePanelController::openDoc(wxString& doc) |
| 348 |
{ |
| 349 |
wxString cmd; |
| 350 |
|
| 351 |
#ifdef __WXMSW__ |
| 352 |
cmd.sprintf(wxT("cmd /c \"start %s\""), doc.c_str()); |
| 353 |
#endif |
| 354 |
|
| 355 |
#ifdef __WXMAC__ |
| 356 |
cmd.sprintf(wxT("open %s"), doc.c_str()); |
| 357 |
#endif |
| 358 |
|
| 359 |
#ifdef __WXCOCOA__ |
| 360 |
cmd.sprintf(wxT("open %s"), doc.c_str()); |
| 361 |
#endif |
| 362 |
|
| 363 |
#ifdef __WXGTK__ |
| 364 |
cmd.sprintf(wxT("gnome-open %s"), doc.c_str()); |
| 365 |
#endif |
| 366 |
|
| 367 |
wxExecute(cmd); |
| 368 |
} |
| 369 |
|
| 370 |
#endif |