2008-08-21 02:33:19 +00:00
|
|
|
/*
|
2009-01-18 15:26:11 +00:00
|
|
|
* Copyright 2008-2009, Haiku, Inc. All Rights Reserved.
|
2008-08-21 02:33:19 +00:00
|
|
|
* Distributed under the terms of the MIT License.
|
2009-01-18 15:26:11 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Bruno Albuquerque, bga@bug-br.org.br
|
2008-08-21 02:33:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cddb_daemon.h"
|
|
|
|
|
2009-05-11 00:55:42 +00:00
|
|
|
#include "cddb_server.h"
|
|
|
|
|
2008-08-21 02:33:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <Directory.h>
|
|
|
|
#include <NodeMonitor.h>
|
2009-05-11 00:55:42 +00:00
|
|
|
#include <Message.h>
|
2008-08-21 02:33:19 +00:00
|
|
|
#include <Volume.h>
|
2009-05-11 00:55:42 +00:00
|
|
|
#include <VolumeRoster.h>
|
2008-08-21 02:33:19 +00:00
|
|
|
|
|
|
|
#include <fs_info.h>
|
2008-08-25 17:43:10 +00:00
|
|
|
#include <stdlib.h>
|
2008-08-21 02:33:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
static const char* kCddaFsName = "cdda";
|
2008-08-25 17:43:10 +00:00
|
|
|
static const int kMaxTocSize = 1024;
|
2008-08-21 02:33:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
CDDBDaemon::CDDBDaemon()
|
2009-01-18 15:26:11 +00:00
|
|
|
: BApplication("application/x-vnd.Haiku-cddb_daemon"),
|
2008-08-21 14:28:20 +00:00
|
|
|
fVolumeRoster(new BVolumeRoster)
|
2008-08-21 02:33:19 +00:00
|
|
|
{
|
2008-08-21 12:37:31 +00:00
|
|
|
fVolumeRoster->StartWatching();
|
2008-08-21 02:33:19 +00:00
|
|
|
|
|
|
|
BVolume volume;
|
2008-08-21 12:37:31 +00:00
|
|
|
while (fVolumeRoster->GetNextVolume(&volume) == B_OK) {
|
2009-05-11 00:55:42 +00:00
|
|
|
if (_Lookup(volume.Device()) != B_OK) {
|
2008-08-25 17:43:10 +00:00
|
|
|
continue;
|
2008-08-21 02:33:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CDDBDaemon::~CDDBDaemon()
|
|
|
|
{
|
2008-08-21 12:37:31 +00:00
|
|
|
fVolumeRoster->StopWatching();
|
|
|
|
delete fVolumeRoster;
|
2008-08-21 02:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
CDDBDaemon::MessageReceived(BMessage* message)
|
|
|
|
{
|
|
|
|
switch(message->what) {
|
|
|
|
case B_NODE_MONITOR:
|
|
|
|
int32 opcode;
|
|
|
|
if (message->FindInt32("opcode", &opcode) == B_OK) {
|
|
|
|
if (opcode == B_DEVICE_MOUNTED) {
|
|
|
|
dev_t device;
|
|
|
|
if (message->FindInt32("new device", &device) == B_OK) {
|
2009-05-11 00:55:42 +00:00
|
|
|
if (_Lookup(device) != B_OK)
|
2008-08-25 17:43:10 +00:00
|
|
|
break;
|
2008-08-21 02:33:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BApplication::MessageReceived(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-11 00:55:42 +00:00
|
|
|
status_t
|
|
|
|
CDDBDaemon::_Lookup(const dev_t device)
|
|
|
|
{
|
|
|
|
scsi_toc_toc* toc = (scsi_toc_toc*)malloc(kMaxTocSize);
|
|
|
|
if (toc == NULL)
|
|
|
|
return B_NO_MEMORY;
|
|
|
|
|
|
|
|
uint32 cddbId;
|
|
|
|
if (!_CanLookup(device, &cddbId, toc)) {
|
|
|
|
free(toc);
|
|
|
|
return B_BAD_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("CD can be looked up. CDDB id = %08lx.\n", cddbId);
|
|
|
|
|
|
|
|
CDDBServer cddb_server("freedb.freedb.org:80");
|
|
|
|
|
|
|
|
status_t result;
|
|
|
|
|
|
|
|
BList queryResponse;
|
|
|
|
if ((result = cddb_server.Query(cddbId, toc, &queryResponse)) != B_OK) {
|
|
|
|
free(toc);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(toc);
|
|
|
|
|
|
|
|
QueryResponseData* diskData = _SelectResult(&queryResponse);
|
|
|
|
if (diskData == NULL) {
|
|
|
|
return B_BAD_INDEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReadResponseData readResponse;
|
|
|
|
if ((result = cddb_server.Read(diskData, &readResponse)) != B_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_WriteCDData(device, diskData, &readResponse) == B_OK) {
|
|
|
|
printf("CD data saved.\n");
|
|
|
|
} else {
|
|
|
|
printf("CD data not saved.\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete itens in the query response BList;
|
|
|
|
int32 count = queryResponse.CountItems();
|
|
|
|
for (int32 i = 0; i < count; ++i) {
|
|
|
|
delete (QueryResponseData*)queryResponse.RemoveItem(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
queryResponse.MakeEmpty();
|
|
|
|
|
|
|
|
// Delete itens in the track data BList in the read response data;
|
|
|
|
count = readResponse.tracks.CountItems();
|
|
|
|
for (int32 i = 0; i < count; ++i) {
|
|
|
|
delete (TrackData*)readResponse.tracks.RemoveItem(0L);
|
|
|
|
}
|
|
|
|
|
|
|
|
readResponse.tracks.MakeEmpty();
|
|
|
|
|
|
|
|
return B_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-21 02:33:19 +00:00
|
|
|
bool
|
2009-01-18 16:05:46 +00:00
|
|
|
CDDBDaemon::_CanLookup(const dev_t device, uint32* cddbId,
|
2009-01-18 15:26:11 +00:00
|
|
|
scsi_toc_toc* toc) const
|
2008-08-21 02:33:19 +00:00
|
|
|
{
|
2009-01-18 15:26:11 +00:00
|
|
|
if (cddbId == NULL || toc == NULL)
|
2008-08-25 17:43:10 +00:00
|
|
|
return false;
|
|
|
|
|
2008-08-21 02:33:19 +00:00
|
|
|
// Is it an Audio disk?
|
|
|
|
fs_info info;
|
2009-01-18 15:26:11 +00:00
|
|
|
fs_stat_dev(device, &info);
|
2008-08-21 02:33:19 +00:00
|
|
|
if (strncmp(info.fsh_name, kCddaFsName, strlen(kCddaFsName)) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Does it have the CD:do_lookup attribute and is it true?
|
2009-01-18 15:26:11 +00:00
|
|
|
BVolume volume(device);
|
2008-08-21 02:33:19 +00:00
|
|
|
BDirectory directory;
|
|
|
|
volume.GetRootDirectory(&directory);
|
|
|
|
|
|
|
|
bool doLookup;
|
|
|
|
if (directory.ReadAttr("CD:do_lookup", B_BOOL_TYPE, 0, (void *)&doLookup,
|
|
|
|
sizeof(bool)) < B_OK || !doLookup)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Does it have the CD:cddbid attribute?
|
2009-01-18 15:26:11 +00:00
|
|
|
if (directory.ReadAttr("CD:cddbid", B_UINT32_TYPE, 0, (void *)cddbId,
|
2008-08-21 02:33:19 +00:00
|
|
|
sizeof(uint32)) < B_OK)
|
|
|
|
return false;
|
|
|
|
|
2008-08-25 17:43:10 +00:00
|
|
|
// Does it have the CD:toc attribute?
|
2009-01-18 15:26:11 +00:00
|
|
|
if (directory.ReadAttr("CD:toc", B_RAW_TYPE, 0, (void *)toc,
|
2008-08-25 17:43:10 +00:00
|
|
|
kMaxTocSize) < B_OK)
|
|
|
|
return false;
|
|
|
|
|
2008-08-21 02:33:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-11 00:55:42 +00:00
|
|
|
QueryResponseData*
|
|
|
|
CDDBDaemon::_SelectResult(BList* response) const
|
|
|
|
{
|
|
|
|
// Select a single CD match from the response and return it.
|
|
|
|
//
|
|
|
|
// TODO(bga):Right now it just picks the first entry on the list but
|
|
|
|
// someday we may want to let the user choose one.
|
|
|
|
if (response->CountItems() != 0)
|
|
|
|
return (QueryResponseData*)response->ItemAt(0L);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status_t
|
|
|
|
CDDBDaemon::_WriteCDData(dev_t device, QueryResponseData* diskData,
|
|
|
|
ReadResponseData* readResponse)
|
|
|
|
{
|
|
|
|
// Rename volume.
|
|
|
|
BVolume volume(device);
|
|
|
|
|
|
|
|
status_t result;
|
|
|
|
if ((result = volume.SetName((diskData->title).String())) != B_OK) {
|
|
|
|
printf("Can't set volume name.\n");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-21 02:33:19 +00:00
|
|
|
int main(void) {
|
|
|
|
CDDBDaemon* cddbDaemon = new CDDBDaemon();
|
|
|
|
cddbDaemon->Run();
|
|
|
|
delete cddbDaemon;
|
|
|
|
}
|