public void LoadFile(string filePath) { // First, ensure a clean state. CloseFile(); this.statusbar1.Pop(_SbCtxState); // Indicate what we are there for early. // // (The idea is that a hypothetically long loading process // with the user placing the window in the background // would show up in the window list identifiably already.) this.FilePath = filePath; // Register file as recently used, so that it can be opened easily // via the File -> Recent submenu. try { this.statusbar1.Push(_SbCtxActivity, string.Format( "Registering file \"{0}\" as recently used...", _FilePath)); Application.RunIteration(false); RecentManager manager = RecentManager.Default; var data = new RecentData(); data.MimeType = "text/plain"; data.AppName = this.RecentApplicationName; // Rather don't record current binary location, // it could be a development version... // But we have to enter something so that Gtk // will let us record the recent info at all, // so we make something up here that will be used // when the program would be installed system-wide... data.AppExec = this.RecentApplicationExec; if (manager.AddFull(_FilePath, data) == false) { // (I, Fabian, wanted to make this an error // that is always somehow reported, but I was told // that someone who disables the recent files list // would not want log info spam in return ... So,) Debug.Print("[MainWindow] LoadFile(): " + "Adding file \"{0}\" to recently used files list did not work.", _FilePath); } } finally { this.statusbar1.Pop(_SbCtxActivity); } // Load file from stable storage. try { this.statusbar1.Push(_SbCtxActivity, string.Format( "Loading file \"{0}\"...", _FilePath)); Application.RunIteration(false); using (var reader = new StreamReader(_FilePath)) { // TODO: Read in text for TextView and for Notes in parallel. // Otherwise, they could diverge... // Read in text for TextView. TextBuffer buf = this.textviewText.Buffer; TextIter end = buf.EndIter; buf.Insert(ref end, reader.ReadToEnd()); // For now, reset the reader to beginning // and read everything in again. // TODO: (Or can/should we read the data from the TextView?) reader.BaseStream.Seek(0, SeekOrigin.Begin); // Parse text as Notes. var notes = new Notes(reader); // Make parse visible to the user. AddNotesTree(notes); } } catch (IOException ex) { VisualizeError("Error processing file \"{0}\": {1}", _FilePath, ex.Message); this.FilePath = null; return; } catch (Exception ex) { VisualizeError("Unexpected error (file was \"{0}\"): {1}", _FilePath, ex.Message); this.FilePath = null; return; } finally { this.statusbar1.Pop(_SbCtxActivity); } // Consider file loaded. this.statusbar1.Push(_SbCtxState, "File loaded."); }
private void OpenDB(string path, bool createNew, bool loadAsync, System.Action onsuccess) { EnableGui(false); // will be re-enabled after opening AND loading has been completed successfully SetWindowTitle(null); // clear views tvVolumes.Clear(); tvItems.Clear(); itemInfo.Clear(); itemInfo.Hide(); if (database != null) { database.Close(); } lastSuccessfulSearchCriteria = null; try { database = new VolumeDatabase(path, createNew); database.SearchItemResultsLimit = App.SEARCH_RESULTS_LIMIT; } catch (UnsupportedDbVersionException) { MsgDialog.ShowError(this, S._("Unsupported database version"), S._("This database version is not supported.")); return; } // load volumes Action <Volume[]> updateGui = (Volume[] volumes) => { tvVolumes.Fill(volumes); // select first volume /* * // this clearly harms startup time. * TreeIter iter; * if (tvVolumes.Model.GetIterFirst(out iter)) * tvVolumes.Selection.SelectIter(iter); */ EnableGui(true); SetWindowTitle(path); SetTempStatus(string.Format(S._("{0} volumes loaded."), volumes.Length)); recentManager.AddFull("file://" + path, recentData); App.Settings.MostRecentDBPath = path; App.Settings.Save(); if (onsuccess != null) { onsuccess(); // must be called on the gui thread } }; if (loadAsync) { // delegate that will be called // when asynchronous volume loading (searching) has been finished. AsyncCallback cb = (IAsyncResult ar) => { Volume[] volumes; try { volumes = database.EndSearchVolume(ar); } catch (Exception ex) { Application.Invoke(delegate { SetStatus(string.Format(S._("An error occured while loading the volume list: {0}"), ex.Message)); }); return; } Application.Invoke(delegate { updateGui(volumes); }); }; database.BeginSearchVolume(cb, null); // returns immediately } else { Volume[] volumes = database.SearchVolume(); updateGui(volumes); } }