1/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* vim:set expandtab ts=4 shiftwidth=4: */
3/*
4 * Copyright (C) 2008 Sun Microsystems, Inc. All rights reserved.
5 * Use is subject to license terms.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * Authors: Lin Ma <lin.ma@sun.com>
23 */
24
25#include "config.h"
26#include <glib.h>
27#include "fen-data.h"
28#include "fen-missing.h"
29
30G_LOCK_EXTERN (fen_lock);
31#define SCAN_MISSING_INTERVAL 4000	/* in milliseconds */
32
33#ifdef GIO_COMPILATION
34#define FM_W if (fm_debug_enabled) g_warning
35gboolean fm_debug_enabled = FALSE;
36#else
37#include "gam_error.h"
38#define FM_W(...) GAM_DEBUG(DEBUG_INFO, __VA_ARGS__)
39#endif
40
41/* global data structure for scan missing files */
42static GList *missing_list = NULL;
43static guint scan_missing_source_id = 0;
44
45static gboolean scan_missing_list (gpointer data);
46
47static gboolean
48scan_missing_list (gpointer data)
49{
50    GList *existing_list = NULL;
51    GList *idx = NULL;
52    fdata *f;
53    gboolean ret = TRUE;
54
55    G_LOCK (fen_lock);
56
57    for (idx = missing_list; idx; idx = idx->next) {
58        f = (fdata*)idx->data;
59
60        if (port_add (&f->fobj, &f->len, f)) {
61            /* TODO - emit CREATE event */
62            fdata_emit_events (f, FN_EVENT_CREATED);
63            existing_list = g_list_prepend (existing_list, idx);
64        }
65    }
66
67    for (idx = existing_list; idx; idx = idx->next) {
68        missing_list = g_list_remove_link (missing_list, (GList *)idx->data);
69        g_list_free_1 ((GList *)idx->data);
70    }
71    g_list_free (existing_list);
72
73    if (missing_list == NULL) {
74        scan_missing_source_id = 0;
75        ret = FALSE;
76    }
77
78    G_UNLOCK (fen_lock);
79    return ret;
80}
81
82/**
83 * missing_add
84 *
85 * Unsafe, need lock fen_lock.
86 */
87void
88missing_add (fdata *f)
89{
90    GList *idx;
91
92    g_assert (!is_ported (f));
93
94    if (g_list_find (missing_list, f) != NULL) {
95        FM_W ("%s is ALREADY added %s\n", __func__, FN_NAME(f));
96        return;
97    }
98    FM_W ("%s is added %s\n", __func__, FN_NAME(f));
99
100    missing_list = g_list_prepend (missing_list, f);
101
102    /* if doesn't scan, then start */
103    if (scan_missing_source_id == 0) {
104        scan_missing_source_id = g_timeout_add (SCAN_MISSING_INTERVAL,
105          scan_missing_list,
106          NULL);
107        g_assert (scan_missing_source_id > 0);
108    }
109}
110
111/**
112 * missing_remove
113 *
114 * Unsafe, need lock fen_lock.
115 */
116void
117missing_remove (fdata *f)
118{
119    FM_W ("%s %s\n", __func__, FN_NAME(f));
120    missing_list = g_list_remove (missing_list, f);
121}
122