Don't offer to delete a corrupt .todo; too much chance for making a mistake, like...
[ttodo.git] / todo.cc
blobd77a2783b507d48c907db8404111760de9a27f42
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 ()
14 /// Singleton interface.
15 /*static*/ CTodoApp& CTodoApp::Instance (void)
17 static CTodoApp s_App;
18 return (s_App);
21 /// Set \p filename to a default value to be used when none is given on the cmdline.
22 void CTodoApp::GetDefaultFilename (string& filename) const
24 filename.clear();
26 // This code gets the first available .todo in the current path.
27 // That is, it will use ./.todo, or ../.todo, etc.
29 // Get the nesting level to know how far back to check.
30 char cwd [PATH_MAX];
31 if (!getcwd (VectorBlock (cwd)))
32 throw libc_exception ("getcwd");
33 const size_t nDirs = count (cwd, cwd + strlen(cwd), '/');
34 assert (nDirs && "A path without root in it?");
36 // Check for existing .todo in each directory.
37 filename.reserve (strlen("../") * nDirs + strlen(".todo"));
38 int i;
39 for (i = 0; i < int(nDirs); ++i)
40 filename += "../";
41 filename += ".todo";
42 for (; i >= 0; --i)
43 if (access (filename.iat (i * strlen("../")), F_OK) == 0)
44 break;
46 // If there are no .todo files, create ./.todo
47 if (i < 0) {
48 i = nDirs - 1;
49 if (access (".", W_OK) != 0) {
50 MessageBox ("You are not allowed to write to this directory");
51 CRootWindow::Instance().Close();
54 filename.erase (filename.begin(), i * strlen("../"));
57 /// Initializes the output device.
58 void CTodoApp::OnCreate (argc_t argc, argv_t argv)
60 CApplication::OnCreate (argc, argv);
61 if (argc > 2) {
62 cerr << "Usage: todo [filename]\n";
63 return (CRootWindow::Instance().Close());
66 // Determine database name.
67 string fullname;
68 if (argc == 2)
69 fullname = argv[1];
70 else
71 GetDefaultFilename (fullname);
73 // Create the frame and register the document with it.
74 CTodoFrame* pFrame = new CTodoFrame;
75 AddChildWindow (pFrame);
76 m_Doc.RegisterView (pFrame);
78 // Open the document.
79 try {
80 m_Doc.Open (fullname);
81 } catch (...) {
82 string msg (fullname);
83 msg += " is corrupt or in an incompatible format.";
84 MessageBox (msg);
85 CRootWindow::Instance().Close();
86 #ifndef NDEBUG
87 throw; // to see the real error for debugging.
88 #endif
92 //----------------------------------------------------------------------
94 WinMain (CTodoApp)
96 //----------------------------------------------------------------------