summaryrefslogtreecommitdiffstats
path: root/kscd/cddbdlg.cpp
blob: e3dd02e0ad26402dbb6e39e5ec8d8a34475f53e2 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <tqkeycode.h>
#include <tqdatetime.h>
#include <textstream.h>
#include <tqfile.h>
#include <tqdir.h>
#include <tqfileinfo.h>
#include <klistview.h>
#include <klineedit.h>
#include <knuminput.h>

#include <kglobal.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kinputdialog.h>
#include <klocale.h>
#include <kcombobox.h>
#include <kmessagebox.h>

#include <stdio.h>
#include <math.h>

#include "version.h"
#include "kscd.h"
#include "cddbdlg.h"
#include "libkcddb/cdinfodialogbase.h"

struct mytoc
{
  unsigned absframe;
};

CDDBDlg::CDDBDlg( TQWidget* parent, const char* name )
    : KDialogBase( parent, name, false, i18n( "CD Editor" ),
      Ok|Cancel|User1|User2, Ok, true )
{
  KGlobal::locale()->insertCatalogue("libkcddb");

  m_dlgBase = new CDInfoDialogBase( this, "m_dlgBase" );

  setMainWidget( m_dlgBase );

  setButtonText( User1, i18n( "Upload" ) );
  setButtonText( User2, i18n( "Fetch Info" ) );

  connect( this, TQT_SIGNAL( okClicked() ), TQT_SLOT( save() ) );
  connect( this, TQT_SIGNAL( user1Clicked() ), TQT_SLOT( upload() ) );
  connect( this, TQT_SIGNAL( user2Clicked() ), TQT_SIGNAL( cddbQuery() ) );
  connect( m_dlgBase, TQT_SIGNAL( play( int ) ), TQT_SIGNAL( play( int ) ) );

  cddbClient = new KCDDB::Client();
  cddbClient->setBlockingMode(false);
  connect (cddbClient, TQT_SIGNAL(finished(CDDB::Result)),
                       TQT_SLOT(submitFinished(CDDB::Result)));
}


CDDBDlg::~CDDBDlg()
{
  delete cddbClient;
}

void CDDBDlg::setData(
  const KCDDB::CDInfo &_cddbInfo,
  const KCDDB::TrackOffsetList &_trackStartFrames,
  const TQStringList &_playlist)
{
    // Let's make a deep copy of the cd struct info so that the data won't
    // change the cd changes while we are playing with the dialog.
    cddbInfo = _cddbInfo;
    trackStartFrames = _trackStartFrames;
    playlist = _playlist;

    // Write the complete record to the dialog.
    m_dlgBase->setInfo(cddbInfo, trackStartFrames);
    // FIXME: KDE4, move this logic into m_dlgBase->setInfo() once KCDDB:CDInfo is updated.
    m_dlgBase->m_playOrder->setText( playlist.join( "," ) );
} // setData

void CDDBDlg::submitFinished(KCDDB::CDDB::Result r)
{
  if (r == KCDDB::CDDB::Success)
  {
    KMessageBox::information(this, i18n("Record submitted successfully."),
         i18n("Record Submission"));
  }
  else
  {
    TQString str = i18n("Error sending record.\n\n%1")
      .arg(KCDDB::CDDB::resultToString(r));
    KMessageBox::error(this, str, i18n("Record Submission"));
  }
} // submitFinished()

void CDDBDlg::upload()
{
    if (!validInfo())
        return;

    updateFromDialog();

    // Create a copy with a bumped revision number.
    KCDDB::CDInfo copyInfo = cddbInfo;
    copyInfo.revision++;
    cddbClient->submit(copyInfo, trackStartFrames);
} // upload

void CDDBDlg::save()
{
    updateFromDialog();

    KCDDB::Cache::store(cddbInfo);

    emit newCDInfoStored(cddbInfo);
} // save

bool CDDBDlg::validInfo()
{
  KCDDB::CDInfo copy = m_dlgBase->info();

  if (copy.artist.isEmpty())
  {
    KMessageBox::sorry(this,
        i18n("The artist name of the disc has to be entered.\n"
             "Please correct the entry and try again."),
        i18n("Invalid Database Entry"));
    return false;
  }

  if (copy.title.isEmpty())
  {
    KMessageBox::sorry(this,
        i18n("The title of the disc has to be entered.\n"
             "Please correct the entry and try again."),
        i18n("Invalid Database Entry"));
    return false;
  }

  bool have_nonempty_title = false;
  for (unsigned i = 0; i < copy.trackInfoList.count(); i++)
  {
      if (!copy.trackInfoList[i].title.isEmpty())
      {
          have_nonempty_title = true;
          break;
      }
  }

  if (!have_nonempty_title)
  {
    KMessageBox::sorry(this,
        i18n("At least one track title must be entered.\n"\
             "Please correct the entry and try again."),
        i18n("Invalid Database Entry"));
    return false;
  }

  return true;
}

void CDDBDlg::updateFromDialog()
{
  KCDDB::CDInfo copy = m_dlgBase->info();

  // Playorder...
  TQStringList strlist = TQStringList::split( ',', m_dlgBase->m_playOrder->text() );

  bool ret = true;

  TQString teststr;
  bool ok;
  unsigned num;

  for ( TQStringList::Iterator it = strlist.begin();
        it != strlist.end();
        ++it )
  {
    teststr = *it;
    num = teststr.toInt(&ok);

    if( !ok || num > cddbInfo.trackInfoList.count() )
      ret = false;
  }

  if(!ret)
  {
    KMessageBox::sorry(this,
        i18n("Invalid Playlist\nPlease use valid track numbers, "
             "separated by commas."));
  }

  cddbInfo = copy;
} // updateFromDialog

#include "cddbdlg.moc"