1 /* nm_filetype_thread.hh
2  * This file belongs to Worker, a file manager for UN*X/X11.
3  * Copyright (C) 2011,2013 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 #ifndef NM_FILETYPE_THREAD_HH
23 #define NM_FILETYPE_THREAD_HH
24 
25 #include <wdefines.h>
26 #include "aguix/thread.hh"
27 #include "aguix/condvar.h"
28 #include <memory>
29 #include <list>
30 #include "fileentry_customcolor.hh"
31 #include "condparser.h"
32 
33 class List;
34 class Worker;
35 class WCFiletype;
36 class WConfig;
37 
38 class NM_Filetype_Thread : public Thread
39 {
40 public:
41     NM_Filetype_Thread();
42     ~NM_Filetype_Thread();
43 
44     void setWorker( Worker *worker );
45 
46     int run();
47 
48     typedef enum { THREAD_RUN,
49                    THREAD_EXIT,
50                    THREAD_STOP } thread_order_t;
51 
52     int switchOrder( thread_order_t neworder );
53 
54     typedef struct check_entry {
check_entryNM_Filetype_Thread::check_entry55         check_entry() : pos( -1 ),
56                         dont_check_content( false ) {}
57         std::string fullname;
58         int pos;
59         bool dont_check_content;
60     } check_entry_t;
61 
62     class check_filetype_result {
63     public:
64         check_entry_t entry;
65         std::vector<unsigned int> ft_index;
66         FileEntryCustomColor custom_color;
67         std::string filetype_file_output;
68         std::string mime_type;
69     };
70 
71     void putRequest( const check_entry_t &req );
72     void clearRequests();
73 
74     void result_list_clear();
75     void result_list_lock();
76     void result_list_unlock();
77     check_filetype_result result_list_remove_locked();
78     bool result_list_is_empty_locked();
79 
80     void copy_filetypes( List *filetypes );
81     void clear_filetypes();
82 
83     // only for master thread
84     static WCFiletype *findFiletype( WConfig *wconfig,
85                                      const std::vector<unsigned int> &v );
86 private:
87 
88     // fifo for ft-results
89     // main thread does remove
90     // slave does put
91     // no size limitation
92     class result_list {
93     public:
94         result_list();
95         ~result_list();
96         result_list( const result_list &other );
97         result_list &operator=( const result_list &other );
98 
99         bool isEmpty_locked();
100         int put_locked( check_filetype_result elem );
101         check_filetype_result remove_locked();
102 
103         void lock();
104         void unlock();
105     protected:
106         std::list< check_filetype_result > entries;
107         MutEx ex;
108     };
109 
110     int slave_checkFiletype( const check_entry_t &entry,
111                              check_filetype_result &res_return );
112     int switchStatus( thread_order_t newstatus );
113 
114     Worker *m_worker;
115 
116     CondParser condparser;
117 
118     int m_running;
119 
120     CondVar m_ordervar_cv;
121     thread_order_t m_order;
122     CondVar m_statusvar_cv;
123     thread_order_t m_status;
124 
125     std::list< check_entry_t > m_entries_to_check;
126     result_list m_result_list;
127 
128     struct {
129         List *filetypes;
130         MutEx filetype_ex;
131     } m_filetype_copy;
132 };
133 
134 #endif
135