summaryrefslogtreecommitdiff
path: root/src/lsof.cpp
blob: 24f14a588e90f504290988c141be281bf13a5976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <config.h>

#include <gtkmm/messagedialog.h>
#include <glib/gi18n.h>
#include <glibtop/procopenfiles.h>

#include <sys/wait.h>

// #include <libsexy/sexy-icon-entry.h>


#include <set>
#include <string>
#include <sstream>
#include <iterator>

#include "regex.h"

#include "procman.h"
#include "lsof.h"
#include "util.h"


using std::string;


namespace
{

  class Lsof
  {
    Glib::RefPtr<Glib::Regex> re;

    bool matches(const string &filename) const
    {
      return this->re->match(filename);
    }

  public:

    Lsof(const string &pattern, bool caseless)
    {
      Glib::RegexCompileFlags flags = static_cast<Glib::RegexCompileFlags>(0);

      if (caseless)
	flags |= Glib::REGEX_CASELESS;

      this->re = Glib::Regex::create(pattern, flags);
    }


    template<typename OutputIterator>
    void search(const ProcInfo &info, OutputIterator out) const
    {
      glibtop_open_files_entry *entries;
      glibtop_proc_open_files buf;

      entries = glibtop_get_proc_open_files(&buf, info.pid);

      for (unsigned i = 0; i != buf.number; ++i) {
	if (entries[i].type & GLIBTOP_FILE_TYPE_FILE) {
	  const string filename(entries[i].info.file.name);
	  if (this->matches(filename))
	    *out++ = filename;
	}
      }

      g_free(entries);
    }
  };



  // GUI Stuff


  enum ProcmanLsof {
    PROCMAN_LSOF_COL_PIXBUF,
    PROCMAN_LSOF_COL_PROCESS,
    PROCMAN_LSOF_COL_PID,
    PROCMAN_LSOF_COL_FILENAME,
    PROCMAN_LSOF_NCOLS
  };


  struct GUI {

    GtkListStore *model;
    GtkEntry *entry;
    GtkWindow *window;
    GtkLabel *count;
    ProcData *procdata;
    bool case_insensitive;


    GUI()
    {
      procman_debug("New Lsof GUI %p", this);
    }


    ~GUI()
    {
      procman_debug("Destroying Lsof GUI %p", this);
    }


    void clear_results()
    {
      gtk_list_store_clear(this->model);
      gtk_label_set_text(this->count, "");
    }


    void clear()
    {
      this->clear_results();
      gtk_entry_set_text(this->entry, "");
    }


    void display_regex_error(const Glib::RegexError& error)
    {
      const char * msg = _("<b>Error</b>\n"
			   "'%s' is not a valid Perl regular expression.\n"
			   "%s");
      std::string message = make_string(g_strdup_printf(msg, this->pattern().c_str(), error.what().c_str()));

      Gtk::MessageDialog dialog(message,
				true, // use markup
				Gtk::MESSAGE_ERROR,
				Gtk::BUTTONS_OK,
				true); // modal
      dialog.run();
    }


    void update_count(unsigned count)
    {
      string s = static_cast<std::ostringstream&>(std::ostringstream() << count).str();
      gtk_label_set_text(this->count, s.c_str());
    }


    string pattern() const
    {
      return gtk_entry_get_text(this->entry);
    }


    void search()
    {
      typedef std::set<string> MatchSet;
      typedef MatchSet::const_iterator iterator;

      this->clear_results();


      try {
	Lsof lsof(this->pattern(), this->case_insensitive);

	unsigned count = 0;

	for (ProcInfo::Iterator it(ProcInfo::begin()); it != ProcInfo::end(); ++it) {
	  const ProcInfo &info(*it->second);

	  MatchSet matches;
	  lsof.search(info, std::inserter(matches, matches.begin()));
	  count += matches.size();

	  for (iterator it(matches.begin()), end(matches.end()); it != end; ++it) {
	    GtkTreeIter file;
	    gtk_list_store_append(this->model, &file);
	    gtk_list_store_set(this->model, &file,
			       PROCMAN_LSOF_COL_PIXBUF, info.pixbuf->gobj(),
			       PROCMAN_LSOF_COL_PROCESS, info.name,
			       PROCMAN_LSOF_COL_PID, info.pid,
			       PROCMAN_LSOF_COL_FILENAME, it->c_str(),
			       -1);
	  }
	}

	this->update_count(count);
      }
      catch (Glib::RegexError& error) {
	this->display_regex_error(error);
      }
    }


    static void search_button_clicked(GtkButton *, gpointer data)
    {
      static_cast<GUI*>(data)->search();
    }


    static void search_entry_activate(GtkEntry *, gpointer data)
    {
      static_cast<GUI*>(data)->search();
    }


    static void clear_button_clicked(GtkButton *, gpointer data)
    {
      static_cast<GUI*>(data)->clear();
    }


    static void close_button_clicked(GtkButton *, gpointer data)
    {
      GUI *gui = static_cast<GUI*>(data);
      gtk_widget_destroy(GTK_WIDGET(gui->window));
      delete gui;
    }


    static void case_button_toggled(GtkToggleButton *button, gpointer data)
    {
      bool state = gtk_toggle_button_get_active(button);
      static_cast<GUI*>(data)->case_insensitive = state;
    }


    static gboolean window_delete_event(GtkWidget *, GdkEvent *, gpointer data)
    {
      delete static_cast<GUI*>(data);
      return FALSE;
    }

  };
}




void procman_lsof(ProcData *procdata)
{
  GtkListStore *model = \
    gtk_list_store_new(PROCMAN_LSOF_NCOLS,
		       GDK_TYPE_PIXBUF,	// PROCMAN_LSOF_COL_PIXBUF
		       G_TYPE_STRING,	// PROCMAN_LSOF_COL_PROCESS
		       G_TYPE_UINT,	// PROCMAN_LSOF_COL_PID
		       G_TYPE_STRING	// PROCMAN_LSOF_COL_FILENAME
		       );

  GtkWidget *tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
  g_object_unref(model);
  gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);


  GtkTreeViewColumn *column;
  GtkCellRenderer *renderer;

  // PIXBUF / PROCESS

  column = gtk_tree_view_column_new();

  renderer = gtk_cell_renderer_pixbuf_new();
  gtk_tree_view_column_pack_start(column, renderer, FALSE);
  gtk_tree_view_column_set_attributes(column, renderer,
				      "pixbuf", PROCMAN_LSOF_COL_PIXBUF,
				      NULL);

  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_column_pack_start(column, renderer, FALSE);
  gtk_tree_view_column_set_attributes(column, renderer,
				      "text", PROCMAN_LSOF_COL_PROCESS,
				      NULL);

  gtk_tree_view_column_set_title(column, _("Process"));
  gtk_tree_view_column_set_sort_column_id(column, PROCMAN_LSOF_COL_PROCESS);
  gtk_tree_view_column_set_resizable(column, TRUE);
  gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
  gtk_tree_view_column_set_min_width(column, 10);
  gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
  gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(model), PROCMAN_LSOF_COL_PROCESS,
				       GTK_SORT_ASCENDING);


  // PID
  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes(_("PID"), renderer,
						    "text", PROCMAN_LSOF_COL_PID,
						    NULL);
  gtk_tree_view_column_set_sort_column_id(column, PROCMAN_LSOF_COL_PID);
  gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
  gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);


  // FILENAME
  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes(_("Filename"), renderer,
						    "text", PROCMAN_LSOF_COL_FILENAME,
						    NULL);
  gtk_tree_view_column_set_sort_column_id(column, PROCMAN_LSOF_COL_FILENAME);
  gtk_tree_view_column_set_resizable(column, TRUE);
  gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
  gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);


  GtkWidget *dialog; /* = gtk_dialog_new_with_buttons(_("Search for Open Files"), NULL,
			GTK_DIALOG_DESTROY_WITH_PARENT,
			GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
			NULL); */
  dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(procdata->app));
  gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
  // gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
  gtk_window_set_title(GTK_WINDOW(dialog), _("Search for Open Files"));

  // g_signal_connect(G_OBJECT(dialog), "response",
  //		   G_CALLBACK(close_dialog), NULL);
  gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
  gtk_window_set_default_size(GTK_WINDOW(dialog), 575, 400);
  // gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
  gtk_container_set_border_width(GTK_CONTAINER(dialog), 12);
  GtkWidget *mainbox = gtk_vbox_new(FALSE, 12);
  gtk_container_add(GTK_CONTAINER(dialog), mainbox);
  gtk_box_set_spacing(GTK_BOX(mainbox), 6);


  // Label, entry and search button

  GtkWidget *hbox1 = gtk_hbox_new(FALSE, 12);
  gtk_box_pack_start(GTK_BOX(mainbox), hbox1, FALSE, FALSE, 0);

  GtkWidget *image = gtk_image_new_from_stock(GTK_STOCK_FIND, GTK_ICON_SIZE_DIALOG);
  gtk_box_pack_start(GTK_BOX(hbox1), image, FALSE, FALSE, 0);


  GtkWidget *vbox2 = gtk_vbox_new(FALSE, 12);
  gtk_box_pack_start(GTK_BOX(hbox1), vbox2, TRUE, TRUE, 0);


  GtkWidget *hbox = gtk_hbox_new(FALSE, 12);
  gtk_box_pack_start(GTK_BOX(vbox2), hbox, TRUE, TRUE, 0);
  GtkWidget *label = gtk_label_new_with_mnemonic(_("_Name contains:"));
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
  GtkWidget *entry = gtk_entry_new();

  // entry = sexy_icon_entry_new();
  // sexy_icon_entry_add_clear_button(SEXY_ICON_ENTRY(entry));
  // GtkWidget *icon = gtk_image_new_from_stock(GTK_STOCK_FIND, GTK_ICON_SIZE_MENU);
  // sexy_icon_entry_set_icon(SEXY_ICON_ENTRY(entry), SEXY_ICON_ENTRY_PRIMARY, GTK_IMAGE(icon));

  gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
  GtkWidget *search_button = gtk_button_new_from_stock(GTK_STOCK_FIND);
  gtk_box_pack_start(GTK_BOX(hbox), search_button, FALSE, FALSE, 0);
  GtkWidget *clear_button = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
  gtk_box_pack_start(GTK_BOX(hbox), clear_button, FALSE, FALSE, 0);


  GtkWidget *case_button = gtk_check_button_new_with_mnemonic(_("Case insensitive matching"));
  GtkWidget *hbox3 = gtk_hbox_new(FALSE, 12);
  gtk_box_pack_start(GTK_BOX(hbox3), case_button, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(vbox2), hbox3, FALSE, FALSE, 0);


  GtkWidget *results_box = gtk_hbox_new(FALSE, 12);
  gtk_box_pack_start(GTK_BOX(mainbox), results_box, FALSE, FALSE, 0);
  GtkWidget *results_label = gtk_label_new_with_mnemonic(_("S_earch results:"));
  gtk_box_pack_start(GTK_BOX(results_box), results_label, FALSE, FALSE, 0);
  GtkWidget *count_label = gtk_label_new(NULL);
  gtk_box_pack_end(GTK_BOX(results_box), count_label, FALSE, FALSE, 0);




  // Scrolled TreeView
  GtkWidget *scrolled = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
				 GTK_POLICY_AUTOMATIC,
				 GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled),
				      GTK_SHADOW_IN);
  gtk_container_add(GTK_CONTAINER(scrolled), tree);
  gtk_box_pack_start(GTK_BOX(mainbox), scrolled, TRUE, TRUE, 0);

  GtkWidget *bottom_box = gtk_hbox_new(FALSE, 12);
  GtkWidget *close_button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
  gtk_box_pack_start(GTK_BOX(mainbox), bottom_box, FALSE, FALSE, 0);
  gtk_box_pack_end(GTK_BOX(bottom_box), close_button, FALSE, FALSE, 0);


  GUI *gui = new GUI; // wil be deleted by the close button or delete-event
  gui->procdata = procdata;
  gui->model = model;
  gui->window = GTK_WINDOW(dialog);
  gui->entry = GTK_ENTRY(entry);
  gui->count = GTK_LABEL(count_label);

  g_signal_connect(G_OBJECT(entry), "activate",
		   G_CALLBACK(GUI::search_entry_activate), gui);
  g_signal_connect(G_OBJECT(clear_button), "clicked",
		   G_CALLBACK(GUI::clear_button_clicked), gui);
  g_signal_connect(G_OBJECT(search_button), "clicked",
		   G_CALLBACK(GUI::search_button_clicked), gui);
  g_signal_connect(G_OBJECT(close_button), "clicked",
		   G_CALLBACK(GUI::close_button_clicked), gui);
  g_signal_connect(G_OBJECT(case_button), "toggled",
		   G_CALLBACK(GUI::case_button_toggled), gui);
  g_signal_connect(G_OBJECT(dialog), "delete-event",
		   G_CALLBACK(GUI::window_delete_event), gui);


  gtk_widget_show_all(dialog);
}