1 // This file is part of a terminal todo application.
3 // Copyright (C) 2006 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
13 //----------------------------------------------------------------------
15 /// Default constructor.
16 CTodoApp::CTodoApp (void)
23 /// Singleton interface.
24 /*static*/ CTodoApp
& CTodoApp::Instance (void)
26 static CTodoApp s_App
;
30 /// Set \p filename to a default value to be used when none is given on the cmdline.
31 void CTodoApp::GetDefaultFilename (string
& filename
) const
35 // This code gets the first available .todo in the current path.
36 // That is, it will use ./.todo, or ../.todo, etc.
38 // Get the nesting level to know how far back to check.
40 if (!getcwd (VectorBlock (cwd
)))
41 throw libc_exception ("getcwd");
42 const size_t nDirs
= count (cwd
, cwd
+ strlen(cwd
), '/');
43 assert (nDirs
&& "A path without root in it?");
45 // Check for existing .todo in each directory.
46 filename
.reserve (strlen("../") * nDirs
+ strlen(".todo"));
48 for (i
= 0; i
< int(nDirs
); ++i
)
52 if (access (filename
.iat (i
* strlen("../")), F_OK
) == 0)
55 // If there are no .todo files, create ./.todo
58 if (access (".", W_OK
) != 0) {
59 MessageBox ("You are not allowed to write to this directory");
60 CRootWindow::Instance().Close();
63 filename
.erase (filename
.begin(), i
* strlen("../"));
66 /// Initializes the output device.
67 void CTodoApp::OnCreate (argc_t argc
, argv_t argv
)
69 CApplication::OnCreate (argc
, argv
);
71 cerr
<< "Usage: todo [filename]\n";
72 return (CRootWindow::Instance().Close());
75 // Determine database name.
80 GetDefaultFilename (fullname
);
82 // Create the frame and register the document with it.
83 CTodoFrame
* pFrame
= new CTodoFrame
;
84 CRootWindow::Instance().AddChild (pFrame
);
85 m_Doc
.RegisterView (&m_Curlist
);
86 m_Curlist
.RegisterView (pFrame
);
90 m_Doc
.Open (fullname
);
92 string
msg (fullname
);
93 msg
+= " is corrupt or in an incompatible format.";
95 CRootWindow::Instance().Close();
97 throw; // to see the real error for debugging.
102 //----------------------------------------------------------------------
106 //----------------------------------------------------------------------