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.



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

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:



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

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 we didn't specify a device name, default to 'iPod'. */
    if (RTEST(device_name) == FALSE) {
        device_name = rb_str_new2("iPod");
    }

    /* Ensure it's a valid device name, if it was given. */
    if (TYPE(device_name) == T_STRING && RSTRING_LEN(device_name) == 0) {
        rb_raise(eRbPodError, "Device name must not be an empty string.");
        return Qnil;
    }

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

        model_matcher = rb_str_new2("/x?[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: /x?[A-Z][0-9]{3}/i");
            return Qnil;
        }
    }

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

    /* 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:



72
73
74
75
# File 'ext/rbpod/database.c', line 72

static VALUE rbpod_database_device_get(VALUE self)
{
    return rb_class_new_instance(1, &self, cRbPodDevice);
}

#filenameString

Returns the path on the file system to the database.

Returns:

  • (String)


83
84
85
86
87
88
# File 'ext/rbpod/database.c', line 83

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)


117
118
119
120
121
122
# File 'ext/rbpod/database.c', line 117

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.



48
49
50
51
# File 'ext/rbpod/database.c', line 48

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)


17
18
19
20
21
22
23
24
25
26
27
# File 'ext/rbpod/database.c', line 17

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)


35
36
37
38
39
40
# File 'ext/rbpod/database.c', line 35

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.



59
60
61
62
63
64
# File 'ext/rbpod/database.c', line 59

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)


96
97
98
99
100
# File 'ext/rbpod/database.c', line 96

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