Class: RbPod::Database

Inherits:
Object
  • Object
show all
Defined in:
ext/rbpod/database.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mount_point) ⇒ RbPod::Database

Loads a new database parsed from the given mount point.



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
# File 'ext/rbpod/database.c', line 142

static VALUE rbpod_database_initialize(VALUE self, VALUE mount_point)
{
    Itdb_iTunesDB *database = TYPED_DATA_PTR(self, Itdb_iTunesDB);
    Itdb_iTunesDB *previous = database;
    GError *error = NULL;

    /* Check if the mount point is a directory. */
    if (rb_file_directory_p(rb_cFile, mount_point) != Qtrue) {
        rb_raise(eRbPodError, "The mount point must be a directory!");
        return Qnil;
    }

    /* Try to parse the database from the given mount point. */
    database = itdb_parse(StringValueCStr(mount_point), &error);

    if (database == NULL) {
        return rbpod_raise_error(error);
    }

    /* Overwrite our old database with the new one. */
    DATA_PTR(self) = database;

    /* Free the old one. */
    itdb_free(previous);

    return self;
}

Class Method Details

.create!(mount_point) ⇒ RbPod::Database .create!(mount_point, device_name) ⇒ RbPod::Database .create!(mount_point, device_name, model_number) ⇒ RbPod::Database

Creates a new database on the file system and loads it.

Overloads:



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
# File 'ext/rbpod/database.c', line 184

static VALUE rbpod_database_create(int argc, VALUE *argv, VALUE self)
{
    gchar *_mount_point, *_device_name, *_model_number;
    VALUE model_matcher, model_regexp, ignorecase;
    VALUE mount_point, device_name, model_number;
    GError *error = NULL;

    if (rb_scan_args(argc, argv, "12", &mount_point, &device_name, &model_number) < 1) {
        rb_raise(eRbPodError, "Invalid arguments.");
        return Qnil;
    }

    /* Check if the mount point is a directory. */
    if (rb_file_directory_p(rb_cFile, mount_point) != Qtrue) {
        rb_raise(eRbPodError, "The mount point must be a directory!");
        return Qnil;
    }

    /* If a device name was given, ensure it's a string. */
    if (RTEST(device_name)) {
        Check_Type(device_name, T_STRING);

        if (RSTRING_LEN(device_name) < 4) {
            rb_raise(eRbPodError, "Device name must be a string of at least four characters.");
            return Qnil;
        }
    }

    /* If a model number was given, ensure it's a string. */
    if (RTEST(model_number)) {
        Check_Type(model_number, T_STRING);

        model_matcher = rb_str_new2("/[xM]?[A-Z][0-9]{3}/");
        ignorecase = rb_const_get(rb_cRegexp, rb_intern("IGNORECASE"));
        model_regexp = rb_reg_new_str(model_matcher, ignorecase);

        if (RTEST(rb_reg_match(model_regexp, model_number)) == FALSE) {
            rb_raise(eRbPodError, "Model number must be a string matching: /[xM]?[A-Z][0-9]{3}/i");
            return Qnil;
        }
    }

    /* Extract pointers for glib use. */
    _mount_point  = StringValueCStr(mount_point);

    /* GPod will use 'iPod' as the device name if it wasn't specified. */
    _device_name  = !NIL_P(device_name) ? StringValueCStr(device_name) : NULL;

    /* GPod can function with a NULL model number, however, artwork may not function properly. */
    _model_number = !NIL_P(model_number) ? StringValueCStr(model_number) : NULL;

    /* Initialize the iPod at this mount point, with this device name and model number. */
    if (itdb_init_ipod(_mount_point, _model_number, _device_name, &error) == FALSE) {
        return rbpod_raise_error(error);
    }

    /* Return an instance of this class using the newly created database. */
    return rb_class_new_instance(1, &mount_point, cRbPodDatabase);
}

Instance Method Details

#deviceRbPod::Device

Returns the device backing this database.

Returns:



66
67
68
69
70
71
# File 'ext/rbpod/database.c', line 66

static VALUE rbpod_database_device_get(VALUE self)
{
    Itdb_iTunesDB *database = TYPED_DATA_PTR(self, Itdb_iTunesDB);
    VALUE mount_point = rb_str_new2(itdb_get_mountpoint(database));
    return rb_class_new_instance(1, &mount_point, cRbPodDevice);
}

#filenameString

Returns the path on the file system to the database.

Returns:

  • (String)


79
80
81
82
83
84
# File 'ext/rbpod/database.c', line 79

static VALUE rbpod_database_filename_get(VALUE self)
{
    Itdb_iTunesDB *database = TYPED_DATA_PTR(self, Itdb_iTunesDB);
    VALUE file_path = rb_str_new2(database->filename);
    return rb_class_new_instance(1, &file_path, rb_cPathname);
}

#mountpointString

Returns the location of the mount point this database was parsed from.

Returns:

  • (String)


113
114
115
116
117
118
# File 'ext/rbpod/database.c', line 113

static VALUE rbpod_database_mountpoint_get(VALUE self)
{
    Itdb_iTunesDB *database = TYPED_DATA_PTR(self, Itdb_iTunesDB);
    VALUE mount_point = rb_str_new2(itdb_get_mountpoint(database));
    return rb_class_new_instance(1, &mount_point, rb_cPathname);
}

#playlistsRbPod::PlaylistCollection

Returns a collection of all playlists added to this database.



42
43
44
45
# File 'ext/rbpod/database.c', line 42

static VALUE rbpod_database_playlists_get(VALUE self)
{
    return rb_class_new_instance(1, &self, cRbPodPlaylistCollection);
}

#save!nil

Saves any changes made to the database.

Returns:

  • (nil)


11
12
13
14
15
16
17
18
19
20
21
# File 'ext/rbpod/database.c', line 11

static VALUE rbpod_database_save(VALUE self)
{
    Itdb_iTunesDB *database = TYPED_DATA_PTR(self, Itdb_iTunesDB);
    GError *error = NULL;

    if (itdb_write(database, &error) == FALSE) {
        return rbpod_raise_error(error);
    }

    return Qnil;
}

#synchronized?Boolean

Returns true or false depending if any changes have been made, but not saved.

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'ext/rbpod/database.c', line 29

static VALUE rbpod_database_synchronized_p(VALUE self)
{
    Itdb_iTunesDB *database = TYPED_DATA_PTR(self, Itdb_iTunesDB);
    guint32 nontransferred = itdb_tracks_number_nontransferred(database);
    return BooleanValue(nontransferred == 0);
}

#tracksRbPod::TrackCollection

Returns a collection of all tracks added to this database.



53
54
55
56
57
58
# File 'ext/rbpod/database.c', line 53

static VALUE rbpod_database_tracks_get(VALUE self)
{
    VALUE playlists = rbpod_database_playlists_get(self);
    VALUE master = rb_funcall(playlists, rb_intern("master"), 0);
    return rb_class_new_instance(1, &master, cRbPodTrackCollection);
}

#versionFixnum

Returns the version number of the database.

Returns:

  • (Fixnum)


92
93
94
95
96
# File 'ext/rbpod/database.c', line 92

static VALUE rbpod_database_version_get(VALUE self)
{
    Itdb_iTunesDB *database = TYPED_DATA_PTR(self, Itdb_iTunesDB);
    return INT2NUM(database->version);
}