summaryrefslogtreecommitdiff
path: root/src/lib/note_list.cpp
blob: dcd39f4ab4052443b05b2258d08734775dae0b2f (plain)
    1 /**
    2  * Copyright (C) 2014 Aaron Ball <nullspoon@iohq.net>
    3  *
    4  * Noteless is free software: you can redistribute it and/or modify
    5  * it under the terms of the GNU General Public License as published by
    6  * the Free Software Foundation, either version 3 of the License, or
    7  * (at your option) any later version.
    8  *
    9  * Noteless is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with noteless.  If not, see <http://www.gnu.org/licenses/>.
   16  */
   17 #include "note_list.h"
   18 
   19 /**
   20  * Constructor
   21  *
   22  * @param string dirname Path to the treasure... er... notes
   23  * @param string ext Extension of the notes to pay attention to
   24  */
   25 note_list::note_list( string dirname, string ext ) {
   26   _dirname = dirname;
   27   _ext = ext;
   28   get_notes( _dirname, _ext, "" );
   29 }
   30 
   31 void note_list::get_notes( string base, string ext, string sub ) {
   32   // Dir pointer
   33   DIR* dp;
   34 
   35   string search_dir = base;
   36   // Append the subdir if necessary
   37   if( sub != "" ) {
   38     // Open the subdir if it's specified
   39     search_dir = base + "/" + sub;
   40   }
   41   dp = opendir( search_dir.c_str() );
   42 
   43   if( dp ) {
   44     // Prepare to iterrate through dir items
   45     struct dirent* item;
   46     while( item = readdir( dp ) ) {
   47       string name = item->d_name;
   48 
   49       path p( base + "/" + sub + "/" + name );
   50 
   51       if( name[0] != '.' ) {
   52         if( p.is_dir() == 1 ) {
   53           // TODO
   54           // I really hate the way I'm doing this repeatedly. Going to need a
   55           // better way.
   56           if( sub != "" ) {
   57             get_notes( base, ext, sub + "/" + name );
   58           } else {
   59             get_notes( base, ext, name );
   60           }
   61         } else {
   62           // This will be replaced later with path.join() when it's implemented
   63           if( sub != "" ) {
   64             add( base, sub + "/" + name );
   65           } else {
   66             add( base, name );
   67           }
   68         }
   69       }
   70     }
   71     closedir( dp );
   72   }
   73 }
   74 
   75 
   76 /**
   77  * Adds a new note item to the list by path.
   78  *
   79  * @param string path Path to the note to be added to the list
   80  *
   81  * @return 0
   82  */
   83 int note_list::add( string base, string name ) {
   84   note n( base, name  );
   85   notes.push_back( n ); 
   86   return 0;
   87 }
   88 
   89 /**
   90  * Opens a new note for editing
   91  *
   92  * @param string editor Path to the editor to use for editing
   93  * @param string note_name Name of the note to be created and edited
   94  *
   95  * @return int Success or failure (always success for now)
   96  */
   97 int note_list::create( string editor, string note_name ) {
   98   string spath = _dirname + '/' + note_name + '.' + _ext;
   99   path p( spath );
  100   note n( p );
  101   n.edit( editor );
  102   return 0;
  103 }
  104 
  105 /**
  106  * Opens the specified note for editing
  107  *
  108  * @param string editor Path to the editor to use for editing
  109  * @param string note_name Name of the note to be edited
  110  */
  111 int note_list::edit( string editor, string note_name ) {
  112   int id = find_note_id( note_name );
  113   if( id != -1 ) {
  114     notes[id].edit( editor );
  115   } else {
  116     cout << "Note \"" << note_name << "\" does not exist." << endl;
  117     return 1;
  118   }
  119   return 0;
  120 }
  121 
  122 /**
  123  * Searches all note *names* (friendly and unfriendly) for the given name and
  124  * returns the note id (not necissarily persistent). If note does not exist,
  125  * returns -1. Can be used for a sort of "note_exists" method as such.
  126  *
  127  * @param string note_name Name of the note to search for
  128  *
  129  * @return int The integer of the note. -1 if note does not exist
  130  */
  131 int note_list::find_note_id( string note_name ) {
  132   for( int i = 0; i < notes.size(); i++ ) {
  133     if( notes[i].name == note_name || notes[i].friendly_name() == note_name ) {
  134       return i;
  135     }
  136   }
  137   return -1;
  138 }
  139 
  140 /**
  141  * Enumerates all note names.
  142  */
  143 vector<string> note_list::enum_names() {
  144   vector<string> names;
  145   for( int i = 0; i < notes.size(); i++ ) {
  146     names.push_back( notes[i].friendly_name() );
  147   }
  148   return names;
  149 }
  150 
  151 /**
  152  * Returns the contents of the requested note
  153  *
  154  * @param string note_name Name of the note to get the contents for
  155  *
  156  * @return vector<string> Contents of the note, broken per line into a vector.
  157  */
  158 int note_list::cat_note( string note_name, vector<string>* pbody ) {
  159   // Check for the requested note
  160   int id = find_note_id( note_name );
  161   if( id != -1 ) {
  162     // Read in the note's contents
  163     notes[id].read();
  164     *pbody = notes[id].body;
  165   } else {
  166     return 1;
  167   }
  168   return 0;
  169 }
  170 
  171 /**
  172  * Deletes the specified note
  173  *
  174  * @param string note_name Name of the note to be deleted
  175  *
  176  * @return int Exit code (1 = error, 0 = success)
  177  */
  178 int note_list::rm( string note_name ) {
  179   int id = find_note_id( note_name );
  180   if( id == -1 ) {
  181     cout << "Note \"" << note_name << "\" does not exist." << endl;
  182     return 1;
  183   } else {
  184     notes[id].rm();
  185   }
  186   return 0;
  187 }
  188 
  189 /**
  190  * TODO
  191  */
  192 vector<string> note_list::find( bool case_sensitive, string term ) {
  193   vector<string> matches;
  194   for( int i = 0; i < notes.size(); i++ ) {
  195     vector<string> note_matches = notes[i].find( case_sensitive, term );
  196     if( note_matches.size() > 0 ) {
  197       for( int m = 0; m < note_matches.size(); m++ ) {
  198         matches.push_back( note_matches[m] );
  199       }
  200     }
  201   }
  202   return matches;
  203 }

Generated by cgit