COOPY » Guide
version 0.6.5
|
00001 00002 #include "MergeFrame.h" 00003 00004 using namespace std; 00005 00006 00007 00008 static wxString conv_c(const char *s) { 00009 return wxString(s, wxConvUTF8); 00010 } 00011 00012 static wxString conv(const std::string& s) { 00013 return wxString(s.c_str(), wxConvUTF8); 00014 } 00015 00016 static std::string conv(const wxString& s) { 00017 return std::string(s.mb_str(wxConvUTF8)); 00018 } 00019 00020 static void show(const wxString& view) { 00021 #ifndef WIN32 00022 wxString view2 = wxT("file://") + view; 00023 ::wxLaunchDefaultBrowser(view2); 00024 #else 00025 ::wxLaunchDefaultBrowser(view); 00026 #endif 00027 } 00028 00029 static void show(const std::string& view) { 00030 wxString x = conv(view); 00031 show(x); 00032 } 00033 00034 00035 IMPLEMENT_CLASS(MergeFrame, wxFrame) 00036 00037 BEGIN_EVENT_TABLE(MergeFrame, wxFrame) 00038 EVT_MENU(ID_Quit, MergeFrame::OnQuit) 00039 EVT_MENU(ID_About, MergeFrame::OnAbout) 00040 EVT_MENU(ID_SaveJob, MergeFrame::OnSaveJob) 00041 EVT_MENU(ID_LoadJob, MergeFrame::OnLoadJob) 00042 EVT_BUTTON(ID_Merge, MergeFrame::OnMerge) 00043 EVT_BUTTON(ID_Quit, MergeFrame::OnQuit) 00044 EVT_CLOSE(MergeFrame::OnExit) 00045 END_EVENT_TABLE() 00046 00047 00048 bool MergeFrame::OnInit() { 00049 00050 //SetIcon(wxIcon((char**)appicon_xpm)); 00051 00052 topsizer = new wxBoxSizer( wxVERTICAL ); 00053 00054 wxBoxSizer *dir_bar = new wxBoxSizer( wxHORIZONTAL ); 00055 00056 wxSizerFlags lflags = 00057 wxSizerFlags(0).Align(wxALIGN_LEFT).Border(wxALL, 10); 00058 00059 action_button = new wxButton( this, ID_Merge, _T("&Merge")); 00060 dir_bar->Add(action_button, 00061 lflags); 00062 00063 dir_bar->Add(new wxButton( this, ID_Quit, _T("&Cancel") ), 00064 lflags); 00065 const char *tips[5] = { 00066 "Select a common ancestor", 00067 "Select main version", 00068 "Select other version", 00069 "Select patch file", 00070 "Select output" 00071 }; 00072 const char *labels[5] = { 00073 "Common ancestor", 00074 "Main version", 00075 "Other version", 00076 "Patch file", 00077 "Output" 00078 }; 00079 const char *tags[5] = { 00080 "Ancestor", 00081 "LocalVersion", 00082 "RemoteVersion", 00083 "Patch", 00084 "Output" 00085 }; 00086 00087 wxConfigBase *pConfig = wxConfigBase::Get(); 00088 for (int i=0; i<5; i++) { 00089 wxBoxSizer *fbar = new wxBoxSizer( wxHORIZONTAL ); 00090 boxes[i] = fbar; 00091 config_tags[i] = conv_c(tags[i]); 00092 //printf("--> %s %s\n", conv(config_tags[i]).c_str(), tags[i]); 00093 wxString path = pConfig->Read(config_tags[i],wxT("")); 00094 //printf(" %s\n", conv(path).c_str()); 00095 files[i] = new wxFilePickerCtrl(this,TEXT_Parent+i, 00096 path, 00097 conv_c(tips[i]), 00098 wxT("*.*"), 00099 wxDefaultPosition, 00100 wxSize(400,-1), 00101 (i==4)?(wxFLP_USE_TEXTCTRL|wxFLP_SAVE|wxFLP_OVERWRITE_PROMPT):(wxFLP_DEFAULT_STYLE|wxFLP_CHANGE_DIR)); 00102 files[i]->SetPath(path); 00103 if (files[i]->GetTextCtrl()) { 00104 files[i]->GetTextCtrl()->SetValue(path); 00105 } 00106 fbar->Add(new wxStaticText(this,-1,conv_c(labels[i]), 00107 wxDefaultPosition, 00108 wxSize(150,-1)),lflags); 00109 fbar->Add(files[i],1,wxGROW|wxALIGN_CENTER_VERTICAL|wxALL); 00110 fbar->SetItemMinSize(files[i],400,50); 00111 topsizer->Add(fbar,1,wxGROW|wxALIGN_CENTER_VERTICAL|wxALL); 00112 } 00113 00114 topsizer->Add(dir_bar,wxSizerFlags(0).Align(wxALIGN_RIGHT)); 00115 00116 SetSizer(topsizer); 00117 topsizer->SetSizeHints(this); 00118 00119 return true; 00120 } 00121 00122 00123 void MergeFrame::OnMerge(wxCommandEvent& event) { 00124 //printf("Merging...\n"); 00125 wxString parent = files[0]->GetPath(); 00126 wxString local = files[1]->GetPath(); 00127 wxString remote = files[2]->GetPath(); 00128 wxString patch = files[3]->GetPath(); 00129 wxString output = files[4]->GetPath(); 00130 wxConfigBase *pConfig = wxConfigBase::Get(); 00131 //printf("CONFIG %ld\n", (long)pConfig); 00132 for (int i=0; i<5; i++) { 00133 wxString key = config_tags[i]; 00134 wxString val = files[i]->GetPath(); 00135 //printf("i is %d : %s -> %s\n", i, conv(key).c_str(), conv(val).c_str()); 00136 pConfig->Write(key,val); 00137 } 00138 pConfig->Flush(); 00139 Options opt((string("ss")+mode).c_str()); 00140 vector<string>& core = opt.core; 00141 if (mode=="merge") { 00142 core.push_back(conv(parent)); 00143 core.push_back(conv(local)); 00144 core.push_back(conv(remote)); 00145 } else if (mode=="diff") { 00146 core.push_back(conv(local)); 00147 core.push_back(conv(remote)); 00148 opt.option_string["mode"] = "hilite"; 00149 } else if (mode=="patch") { 00150 core.push_back(conv(local)); 00151 core.push_back(conv(patch)); 00152 } 00153 opt.option_string["output"] = conv(output); 00154 00155 Diff app; 00156 int result = app.apply(opt); 00157 00158 if (result==0) { 00159 wxMessageDialog msg(NULL, wxT("Done! View result?"), 00160 wxT("Done!"),wxYES_NO|wxCANCEL); 00161 if (msg.ShowModal()==wxID_YES) { 00162 show(output); 00163 } 00164 } else { 00165 wxMessageDialog msg(NULL, wxT("That did not work out."), 00166 wxT("Sorry!")); 00167 msg.ShowModal(); 00168 } 00169 00170 /* 00171 printf("Parent %s\n", conv(parent).c_str()); 00172 printf("Local %s\n", conv(local).c_str()); 00173 printf("Remote %s\n", conv(remote).c_str()); 00174 PolyBook parent_sheet, local_sheet, remote_sheet; 00175 parent_sheet.read(conv(parent).c_str()); 00176 local_sheet.read(conv(local).c_str()); 00177 remote_sheet.read(conv(remote).c_str()); 00178 CompareFlags flags; 00179 00180 00181 BookCompare cmp; 00182 MergeOutputAccum accum; 00183 cmp.compare(parent_sheet,local_sheet,remote_sheet,accum,flags); 00184 const DataSheet& result = accum.getSheet(); 00185 printf("Result of dimensions %dx%d\n", result.width(), result.height()); 00186 00187 if (result.width()==0||result.height()==0) { 00188 wxMessageDialog msg(NULL, wxT("No merge took place.\nAre all files present and readable?"), 00189 wxT("Sorry, no merge")); 00190 msg.ShowModal(); 00191 return; 00192 } 00193 00194 wxFileDialog *SaveDialog = new wxFileDialog(this, _("Save Merged Result As _?"), wxEmptyString, wxEmptyString, 00195 _("*.*"), 00196 wxFD_SAVE | wxFD_OVERWRITE_PROMPT, wxDefaultPosition); 00197 00198 if (SaveDialog->ShowModal() == wxID_OK) { 00199 SetTitle(wxString(wxT("Merge - ")) << SaveDialog->GetFilename()); 00200 } else { 00201 return; 00202 } 00203 00204 if (CsvFile::write(result,conv(SaveDialog->GetPath()).c_str())!=0) { 00205 wxMessageDialog msg(NULL, wxT("Could not save file."), 00206 wxT("Sorry, no save")); 00207 msg.ShowModal(); 00208 return; 00209 } 00210 */ 00211 } 00212 00213 00214 MergeFrame::MergeFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size) 00215 : wxFrame(parent, -1, title, pos, size) 00216 { 00217 00218 wxMenu *menuFile = new wxMenu; 00219 00220 //menuFile->Append( ID_LoadJob, _T("&Load Job...") ); 00221 //menuFile->Append( ID_SaveJob, _T("&Save Job...") ); 00222 //menuFile->Append( ID_About, _T("&About...") ); 00223 menuFile->AppendSeparator(); 00224 menuFile->Append( ID_Quit, _T("&Finish") ); 00225 00226 wxMenuBar *menuBar = new wxMenuBar; 00227 menuBar->Append( menuFile, _T("&Job") ); 00228 00229 SetMenuBar( menuBar ); 00230 00231 CreateStatusBar(); 00232 SetStatusText( _T("Compare and contrast") ); 00233 } 00234 00235 void MergeFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) 00236 { 00237 printf("Quit!\n"); 00238 //wxCloseEvent ev; 00239 //wxPostEvent(this,ev); 00240 Show(FALSE); 00241 //Close(TRUE); 00242 } 00243 00244 void MergeFrame::OnLoadJob(wxCommandEvent& WXUNUSED(event)) 00245 { 00246 //wxFileInputStream stream("config.ini"); 00247 //wxFileConfig config(stream); 00248 //wxString a = config.Read("a", ""); 00249 } 00250 00251 void MergeFrame::OnSaveJob(wxCommandEvent& WXUNUSED(event)) 00252 { 00253 //wxFileOutputStream stream("config.ini"); 00254 //wxFileConfig 00255 } 00256 00257 void MergeFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) 00258 { 00259 wxMessageBox(_T("Welcome to ssmerge!\n\ 00260 Now go back to work."), 00261 _T("About ssmerge"), wxOK | wxICON_INFORMATION, this); 00262 } 00263 00264 void MergeFrame::SetMode(const char *mode) { 00265 this->mode = mode; 00266 string action = "&Merge"; 00267 bool shows[5] = { true, true, true, true, true }; 00268 if (this->mode=="merge") { 00269 action = "&Merge"; 00270 shows[3] = false; 00271 } 00272 if (this->mode=="diff") { 00273 action = "&Compare"; 00274 shows[0] = false; 00275 shows[3] = false; 00276 } 00277 if (this->mode=="patch") { 00278 action = "&Patch"; 00279 shows[0] = false; 00280 shows[2] = false; 00281 } 00282 for (int i=0; i<5; i++) { 00283 wxSizerItemList& lst = boxes[i]->GetChildren(); 00284 for (wxSizerItemList::iterator iter = lst.begin(); 00285 iter != lst.end(); iter++) { 00286 (*iter)->Show(shows[i]); 00287 } 00288 boxes[i]->Layout(); 00289 } 00290 action_button->SetLabel(conv(action)); 00291 topsizer->Layout(); 00292 }