Updated bsconf to the latest from uSTL
[ttodo.git] / todo.cc
blob242830c65b12e1da7cf4c8d1b19cdca13ffff219
1 // This file is part of a terminal todo application.
2 //
3 // Copyright (C) 2006 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // todo.cc
7 //
9 #include "todo.h"
10 #include "frame.h"
11 #include <unistd.h>
13 //----------------------------------------------------------------------
15 /// Default constructor.
16 CTodoApp::CTodoApp (void)
17 : CApplication (),
18 m_Doc (),
19 m_Curlist ()
23 /// Singleton interface.
24 /*static*/ CTodoApp& CTodoApp::Instance (void)
26 static CTodoApp s_App;
27 return (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
33 filename.clear();
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.
39 char cwd [PATH_MAX];
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"));
47 int i;
48 for (i = 0; i < int(nDirs); ++i)
49 filename += "../";
50 filename += ".todo";
51 for (; i >= 0; --i)
52 if (access (filename.iat (i * strlen("../")), F_OK) == 0)
53 break;
55 // If there are no .todo files, create ./.todo
56 if (i < 0) {
57 i = nDirs - 1;
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);
70 if (argc > 2) {
71 cerr << "Usage: todo [filename]\n";
72 return (CRootWindow::Instance().Close());
75 // Determine database name.
76 string fullname;
77 if (argc == 2)
78 fullname = argv[1];
79 else
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);
88 // Open the document.
89 try {
90 m_Doc.Open (fullname);
91 } catch (...) {
92 string msg (fullname);
93 msg += " is corrupt or in an incompatible format.";
94 MessageBox (msg);
95 CRootWindow::Instance().Close();
96 #ifndef NDEBUG
97 throw; // to see the real error for debugging.
98 #endif
102 //----------------------------------------------------------------------
104 WinMain (CTodoApp)
106 //----------------------------------------------------------------------