Split the document into the base document and the current list view document.
[ttodo.git] / todo.cc
blob7d6b199050baf1e6ea7cf386c6d45b4ae0ac7517
1 #include "todo.h"
2 #include "frame.h"
3 #include <unistd.h>
5 //----------------------------------------------------------------------
7 /// Default constructor.
8 CTodoApp::CTodoApp (void)
9 : CApplication (),
10 m_Doc (),
11 m_Curlist ()
15 /// Singleton interface.
16 /*static*/ CTodoApp& CTodoApp::Instance (void)
18 static CTodoApp s_App;
19 return (s_App);
22 /// Set \p filename to a default value to be used when none is given on the cmdline.
23 void CTodoApp::GetDefaultFilename (string& filename) const
25 filename.clear();
27 // This code gets the first available .todo in the current path.
28 // That is, it will use ./.todo, or ../.todo, etc.
30 // Get the nesting level to know how far back to check.
31 char cwd [PATH_MAX];
32 if (!getcwd (VectorBlock (cwd)))
33 throw libc_exception ("getcwd");
34 const size_t nDirs = count (cwd, cwd + strlen(cwd), '/');
35 assert (nDirs && "A path without root in it?");
37 // Check for existing .todo in each directory.
38 filename.reserve (strlen("../") * nDirs + strlen(".todo"));
39 int i;
40 for (i = 0; i < int(nDirs); ++i)
41 filename += "../";
42 filename += ".todo";
43 for (; i >= 0; --i)
44 if (access (filename.iat (i * strlen("../")), F_OK) == 0)
45 break;
47 // If there are no .todo files, create ./.todo
48 if (i < 0) {
49 i = nDirs - 1;
50 if (access (".", W_OK) != 0) {
51 MessageBox ("You are not allowed to write to this directory");
52 CRootWindow::Instance().Close();
55 filename.erase (filename.begin(), i * strlen("../"));
58 /// Initializes the output device.
59 void CTodoApp::OnCreate (argc_t argc, argv_t argv)
61 CApplication::OnCreate (argc, argv);
62 if (argc > 2) {
63 cerr << "Usage: todo [filename]\n";
64 return (CRootWindow::Instance().Close());
67 // Determine database name.
68 string fullname;
69 if (argc == 2)
70 fullname = argv[1];
71 else
72 GetDefaultFilename (fullname);
74 // Create the frame and register the document with it.
75 CTodoFrame* pFrame = new CTodoFrame;
76 CRootWindow::Instance().AddChild (pFrame);
77 m_Doc.RegisterView (&m_Curlist);
78 m_Curlist.RegisterView (pFrame);
80 // Open the document.
81 try {
82 m_Doc.Open (fullname);
83 } catch (...) {
84 string msg (fullname);
85 msg += " is corrupt or in an incompatible format.";
86 MessageBox (msg);
87 CRootWindow::Instance().Close();
88 #ifndef NDEBUG
89 throw; // to see the real error for debugging.
90 #endif
94 //----------------------------------------------------------------------
96 WinMain (CTodoApp)
98 //----------------------------------------------------------------------