File: WatchFoldersManager.cpp

package info (click to toggle)
mediaconch 25.04-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,828 kB
  • sloc: ansic: 126,293; cpp: 39,636; javascript: 34,300; xml: 2,950; sh: 2,121; makefile: 200; python: 183
file content (157 lines) | stat: -rw-r--r-- 4,580 bytes parent folder | download | duplicates (3)
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
/*  Copyright (c) MediaArea.net SARL. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license that can
 *  be found in the License.html file in the root of the source tree.
 */

//---------------------------------------------------------------------------
#include "WatchFoldersManager.h"
#include "WatchFolder.h"
#include "Core.h"

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// Watch Folders Manager
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

namespace MediaConch {

//---------------------------------------------------------------------------
WatchFoldersManager::WatchFoldersManager(Core *c) : core(c)
{
}

WatchFoldersManager::~WatchFoldersManager()
{
    CS.Enter();
    std::map<std::string, WatchFolder*>::iterator it = watch_folders.begin();
    for (; it != watch_folders.end(); ++it)
    {
        if (it->second)
        {
            delete it->second;
            it->second = NULL;
        }
    }
    watch_folders.clear();
    CS.Leave();
}

//---------------------------------------------------------------------------
std::map<std::string, std::string> WatchFoldersManager::get_watch_folders()
{
    std::map<std::string, std::string> tmp;
    CS.Enter();
    std::map<std::string, WatchFolder*>::iterator it = watch_folders.begin();
    for (; it != watch_folders.end(); ++it)
    {
        if (!it->second)
            continue;
        tmp[it->first] = it->second->folder_reports;
    }
    CS.Leave();
    return tmp;
}

//---------------------------------------------------------------------------
int WatchFoldersManager::add_watch_folder(const std::string& folder, const std::string& folder_reports,
                                          const std::vector<std::string>& plugins, const std::vector<std::string>& policies,
                                          long *in_user, bool recursive,
                                          const std::vector<std::pair<std::string,std::string> >& options,
                                          long& user_id, std::string& error)
{
    CS.Enter();
    std::map<std::string, WatchFolder*>::iterator it = watch_folders.find(folder);

    if (it != watch_folders.end())
    {
        error = "Already watching this folder";
        CS.Leave();
        return -1;
    }

    if (in_user)
        user_id = *in_user;
    else
    {
        std::vector<long> ids;
        std::string err;
        core->get_users_ids(ids, err);
        for (user_id = -2; true; --user_id)
        {
            bool exists = false;
            for (size_t i = 0; i < ids.size(); ++i)
            {
                if (ids[i] == user_id)
                {
                    exists = true;
                    break;
                }
            }
            if (!exists)
                break;
        }
    }

    WatchFolder *wf = new WatchFolder(core, user_id);
    wf->folder = folder;
    wf->folder_reports = folder_reports;
    wf->set_recursive(recursive);

    for (size_t i = 0; i < plugins.size(); ++i)
        wf->plugins.push_back(plugins[i]);

    for (size_t i = 0; i < policies.size(); ++i)
        wf->policies.push_back(policies[i]);

    for (size_t i = 0; i < options.size(); ++i)
        wf->options.push_back(std::make_pair(options[i].first, options[i].second));

    watch_folders[folder] = wf;
    wf->Run();
    CS.Leave();
    return 0;
}

//---------------------------------------------------------------------------
int WatchFoldersManager::edit_watch_folder(const std::string& folder,
                                           const std::string& folder_reports,
                                           std::string& error)
{
    CS.Enter();
    std::map<std::string, WatchFolder*>::iterator it = watch_folders.find(folder);

    if (it == watch_folders.end() || !it->second)
    {
        error = "Not watching this folder";
        CS.Leave();
        return -1;
    }

    it->second->folder_reports = folder_reports;
    CS.Leave();
    return 0;
}

//---------------------------------------------------------------------------
int WatchFoldersManager::remove_watch_folder(const std::string& folder, std::string& error)
{
    CS.Enter();
    std::map<std::string, WatchFolder*>::iterator it = watch_folders.find(folder);

    if (it == watch_folders.end() || !it->second)
    {
        error = "Not watching this folder";
        CS.Leave();
        return -1;
    }

    delete it->second;
    it->second = NULL;
    watch_folders.erase(it);
    CS.Leave();
    return 0;
}

};