File: eca-resources.cpp

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (167 lines) | stat: -rw-r--r-- 4,982 bytes parent folder | download | duplicates (6)
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
// ------------------------------------------------------------------------
// eca_resources.cpp: User settings (ecasoundrc)
// Copyright (C) 1999-2002,2004,2005,2007 Kai Vehmanen
//
// Attributes:
//     eca-style-version: 3
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string>
#include <cstdlib> /* getenv */

#include "kvu_dbc.h"

#include "eca-logger.h"
#include "eca-resources.h"
#include "eca-version.h"
#include "resource-file.h"

using std::string;

std::string ECA_RESOURCES::rc_override_file;

ECA_RESOURCES::ECA_RESOURCES(void) 
  : resources_found_rep(true)
{

  if (ECA_RESOURCES::rc_override_file.size() == 0) {
    /* case: load global and user resource files normally */
    string ecasound_resource_path (ECA_PKGDATADIR);

    globalrc_repp = new RESOURCE_FILE(ecasound_resource_path + "/ecasoundrc");
    if (globalrc_repp->keywords().size() == 0) {
      ECA_LOG_MSG(ECA_LOGGER::user_objects, "WARNING: Global resource file \"" + ecasound_resource_path + "/ecasoundrc" + "\" not available! Ecasound may not function properly!");
      resources_found_rep = false;
    }
    
    char* home_dir = getenv("HOME");
    if (home_dir != NULL) {
      string user_ecasoundrc_path = string(home_dir) + "/.ecasound";

      user_resource_directory_rep = user_ecasoundrc_path;

      userrc_repp = new RESOURCE_FILE(user_ecasoundrc_path + "/ecasoundrc");
      if (userrc_repp && userrc_repp->has("user-resource-directory") == true) {
	ECA_LOG_MSG(ECA_LOGGER::info, "WARNING: Old resource data found in \"" + user_ecasoundrc_path + "\". You can reset configuration parameters by removing the old rc-file.");
      }
    }
    else
      userrc_repp = 0;

    overriderc_repp = 0;
  }
  else {
    /* case: override, do not use global/user resource files */

    globalrc_repp = userrc_repp = 0;

    overriderc_repp = new RESOURCE_FILE(ECA_RESOURCES::rc_override_file);
    if (overriderc_repp->keywords().size() == 0) {
      ECA_LOG_MSG(ECA_LOGGER::user_objects, "WARNING: Override resource file \"" + ECA_RESOURCES::rc_override_file + "\" not available! Ecasound may not function properly!");
      resources_found_rep = false;
    }
  }
}

ECA_RESOURCES::~ECA_RESOURCES(void)
{
  if (userrc_repp && userrc_repp->is_modified() == true) {
    userrc_repp->resource("ecasound-version", ecasound_library_version);
    userrc_repp->save();
  }

  if (globalrc_repp != 0)
    delete globalrc_repp;

  if (userrc_repp != 0)
    delete userrc_repp;

  if (overriderc_repp != 0)
    delete overriderc_repp;
}

/**
 * Set resource 'tag' value to 'value'. If value wasn't 
 * previously defined, it's added.
 */
void ECA_RESOURCES::resource(const string& tag, const string& value)
{
  if (userrc_repp)
    userrc_repp->resource(tag, value);
}

/**
 * Returns value of resource 'tag'. Priority is given
 * to user-specified resources over global resources.
 */
string ECA_RESOURCES::resource(const string& tag) const
{
  if (tag == "user-resource-directory") 
    return user_resource_directory_rep;
  
  if (userrc_repp && userrc_repp->has(tag))
    return userrc_repp->resource(tag);
  
  if (globalrc_repp && globalrc_repp->has(tag))
    return globalrc_repp->resource(tag);

  if (overriderc_repp && overriderc_repp->has(tag))
    return overriderc_repp->resource(tag);

  return string();
}

/**
 * Returns true if resource 'tag' is 'true', otherwise false
 */
bool ECA_RESOURCES::boolean_resource(const string& tag) const
{
  if (userrc_repp && userrc_repp->has(tag))
    return userrc_repp->boolean_resource(tag);

  if (globalrc_repp && globalrc_repp->has(tag))
    return globalrc_repp->boolean_resource(tag);

  if (overriderc_repp && overriderc_repp->has(tag))
    return overriderc_repp->boolean_resource(tag);

  return false;
}
  
/**
 * Whether resource 'tag' is specified in the resource file
 */
bool ECA_RESOURCES::has(const string& tag) const
{
  if ((globalrc_repp && globalrc_repp->has(tag)) || 
      (userrc_repp && userrc_repp->has(tag)) ||
      (overriderc_repp && overriderc_repp->has(tag))) return true;

  return false;
}

/**
 * Are any resource values available?
 */
bool ECA_RESOURCES::has_any(void) const
{
  return resources_found_rep;
}