1 /* searchop_resultstore.cc
2  * This file belongs to Worker, a file manager for UN*X/X11.
3  * Copyright (C) 2006,2011 Ralf Hoffmann.
4  * You can contact me at: ralf@boomerangsworld.de
5  *   or http://www.boomerangsworld.de/worker
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #include "resultstore.hh"
23 
ResultStore(const std::string & searchname,bool follow_symlinks,bool search_vfs,bool search_same_fs)24 ResultStore::ResultStore( const std::string &searchname, bool follow_symlinks, bool search_vfs, bool search_same_fs )
25 {
26   _dir = new NWC::VirtualDir( searchname, follow_symlinks, false, search_same_fs );
27   _res = new std::list<SearchThread::SearchResult>;
28 
29   m_p = NULL;
30 }
31 
~ResultStore()32 ResultStore::~ResultStore()
33 {
34   delete _dir;
35   delete _res;
36 }
37 
getDir()38 NWC::VirtualDir *ResultStore::getDir()
39 {
40   //TODO returning pointer sucks
41   return _dir;
42 }
43 
getRes()44 std::list<SearchThread::SearchResult> *ResultStore::getRes()
45 {
46   //TODO returning pointer sucks
47   return _res;
48 }
49 
getClonedDir() const50 NWC::VirtualDir *ResultStore::getClonedDir() const
51 {
52   NWC::VirtualDir *d = NULL;
53   NWC::FSEntry *fse = _dir->clone();
54 
55   if ( fse != NULL ) {
56     d = dynamic_cast<NWC::VirtualDir*>( fse );
57   }
58 
59   if ( d == NULL ) {
60       d = new NWC::VirtualDir( "searchdir1", _dir->getFollowSymlinks(), false, _dir->getSearchSameFS() );
61   }
62   return d;
63 }
64 
setSearchSettings(const SearchSettings & sets)65 void ResultStore::setSearchSettings( const SearchSettings &sets )
66 {
67   _settings = sets;
68   _dir->setFollowSymlinks( sets.getFollowSymlinks() );
69   _dir->setSearchVFS( false );
70   _dir->setSearchSameFS( sets.getSearchSameFS() );
71 }
72 
getSearchSettings() const73 SearchSettings ResultStore::getSearchSettings() const
74 {
75   return _settings;
76 }
77 
setActiveRow(int row)78 void ResultStore::setActiveRow( int row )
79 {
80   _settings.setActiveRow( row );
81 }
82 
removeEntries(const std::list<std::pair<std::string,int>> & entry_list)83 void ResultStore::removeEntries( const std::list< std::pair< std::string, int > > &entry_list )
84 {
85     for ( std::list< std::pair< std::string, int > >::const_iterator it = entry_list.begin();
86         it != entry_list.end();
87         it++ ) {
88       removeEntry( it->first, it->second );
89   }
90 }
91 
removeEntry(const std::string & entry,int line)92 void ResultStore::removeEntry( const std::string &entry, int line )
93 {
94     // firstly remove from _res all entries with equal fullname
95     // then remove an entry in the dir with the fullname
96 
97     bool entry_with_different_line_exists = false;
98 
99     for ( std::list<SearchThread::SearchResult>::iterator it = _res->begin();
100           it != _res->end(); ) {
101         if ( (*it).getFullname() == entry ) {
102             if ( line < 0 || it->getLineNr() == line ) {
103                 std::list<SearchThread::SearchResult>::iterator erase_it = it;
104                 it++;
105 
106                 _res->erase( erase_it );
107             } else {
108                 it++;
109                 entry_with_different_line_exists = true;
110             }
111         } else {
112             it++;
113         }
114     }
115 
116     if ( ! entry_with_different_line_exists ) {
117         // no other entry with same full name left so remove it from the list
118         _dir->removeFromList( entry );
119     }
120 }
121 
set_exclusive(void * p)122 bool ResultStore::set_exclusive( void *p )
123 {
124     if ( m_p != NULL ) return false;
125 
126     m_p = p;
127 
128     return true;
129 }
130 
unset_exclusive(void * p)131 void ResultStore::unset_exclusive( void *p )
132 {
133     if ( m_p == p ) {
134         m_p = NULL;
135     }
136 }
137 
is_exclusive() const138 bool ResultStore::is_exclusive() const
139 {
140     if ( m_p ) return true;
141 
142     return false;
143 }
144