/[svn]/linuxsampler/trunk/src/network/lscpserver.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/network/lscpserver.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1161 by iliev, Mon Apr 16 15:51:18 2007 UTC revision 1403 by iliev, Fri Oct 12 09:12:22 2007 UTC
# Line 36  Line 36 
36  #include "../drivers/audio/AudioOutputDeviceFactory.h"  #include "../drivers/audio/AudioOutputDeviceFactory.h"
37  #include "../drivers/midi/MidiInputDeviceFactory.h"  #include "../drivers/midi/MidiInputDeviceFactory.h"
38    
39    
40    /**
41     * Returns a copy of the given string where all special characters are
42     * replaced by LSCP escape sequences ("\xHH"). This function shall be used
43     * to escape LSCP response fields in case the respective response field is
44     * actually defined as using escape sequences in the LSCP specs.
45     *
46     * @e Caution: DO NOT use this function for escaping path based responses,
47     * use the Path class (src/common/Path.h) for this instead!
48     */
49    static String _escapeLscpResponse(String txt) {
50        for (int i = 0; i < txt.length(); i++) {
51            const char c = txt.c_str()[i];
52            if (
53                !(c >= '0' && c <= '9') &&
54                !(c >= 'a' && c <= 'z') &&
55                !(c >= 'A' && c <= 'Z') &&
56                !(c == ' ') && !(c == '!') && !(c == '#') && !(c == '$') &&
57                !(c == '%') && !(c == '&') && !(c == '(') && !(c == ')') &&
58                !(c == '*') && !(c == '+') && !(c == ',') && !(c == '-') &&
59                !(c == '.') && !(c == '/') && !(c == ':') && !(c == ';') &&
60                !(c == '<') && !(c == '=') && !(c == '>') && !(c == '?') &&
61                !(c == '@') && !(c == '[') && !(c == ']') &&
62                !(c == '^') && !(c == '_') && !(c == '`') && !(c == '{') &&
63                !(c == '|') && !(c == '}') && !(c == '~')
64            ) {
65                // convert the "special" character into a "\xHH" LSCP escape sequence
66                char buf[5];
67                snprintf(buf, sizeof(buf), "\\x%02x", static_cast<unsigned char>(c));
68                txt.replace(i, 1, buf);
69                i += 3;
70            }
71        }
72        return txt;
73    }
74    
75  /**  /**
76   * Below are a few static members of the LSCPServer class.   * Below are a few static members of the LSCPServer class.
77   * The big assumption here is that LSCPServer is going to remain a singleton.   * The big assumption here is that LSCPServer is going to remain a singleton.
# Line 52  Line 88 
88  fd_set LSCPServer::fdSet;  fd_set LSCPServer::fdSet;
89  int LSCPServer::currentSocket = -1;  int LSCPServer::currentSocket = -1;
90  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();  std::vector<yyparse_param_t> LSCPServer::Sessions = std::vector<yyparse_param_t>();
91    std::vector<yyparse_param_t>::iterator itCurrentSession = std::vector<yyparse_param_t>::iterator();
92  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedNotifies = std::map<int,String>();
93  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();  std::map<int,String> LSCPServer::bufferedCommands = std::map<int,String>();
94  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();  std::map< LSCPEvent::event_t, std::list<int> > LSCPServer::eventSubscriptions = std::map< LSCPEvent::event_t, std::list<int> >();
# Line 84  LSCPServer::LSCPServer(Sampler* pSampler Line 121  LSCPServer::LSCPServer(Sampler* pSampler
121      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_dir_info, "DB_INSTRUMENT_DIRECTORY_INFO");
122      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_count, "DB_INSTRUMENT_COUNT");
123      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_db_instr_info, "DB_INSTRUMENT_INFO");
124        LSCPEvent::RegisterEvent(LSCPEvent::event_db_instrs_job_info, "DB_INSTRUMENTS_JOB_INFO");
125      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");      LSCPEvent::RegisterEvent(LSCPEvent::event_misc, "MISCELLANEOUS");
126      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");      LSCPEvent::RegisterEvent(LSCPEvent::event_total_voice_count, "TOTAL_VOICE_COUNT");
127      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");      LSCPEvent::RegisterEvent(LSCPEvent::event_global_info, "GLOBAL_INFO");
# Line 144  void LSCPServer::EventHandler::TotalVoic Line 182  void LSCPServer::EventHandler::TotalVoic
182    
183  #if HAVE_SQLITE3  #if HAVE_SQLITE3
184  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {  void LSCPServer::DbInstrumentsEventHandler::DirectoryCountChanged(String Dir) {
185      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, Dir));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_count, InstrumentsDb::toEscapedPath(Dir)));
186  }  }
187    
188  void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {  void LSCPServer::DbInstrumentsEventHandler::DirectoryInfoChanged(String Dir) {
189      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, Dir));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, InstrumentsDb::toEscapedPath(Dir)));
190  }  }
191    
192  void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {  void LSCPServer::DbInstrumentsEventHandler::DirectoryNameChanged(String Dir, String NewName) {
193      Dir = "'" + Dir + "'";      Dir = "'" + InstrumentsDb::toEscapedPath(Dir) + "'";
194      NewName = "'" + NewName + "'";      NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
195      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_dir_info, "NAME", Dir, NewName));
196  }  }
197    
198  void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {  void LSCPServer::DbInstrumentsEventHandler::InstrumentCountChanged(String Dir) {
199      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, Dir));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_count, InstrumentsDb::toEscapedPath(Dir)));
200  }  }
201    
202  void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {  void LSCPServer::DbInstrumentsEventHandler::InstrumentInfoChanged(String Instr) {
203      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, Instr));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, InstrumentsDb::toEscapedPath(Instr)));
204  }  }
205    
206  void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {  void LSCPServer::DbInstrumentsEventHandler::InstrumentNameChanged(String Instr, String NewName) {
207      Instr = "'" + Instr + "'";      Instr = "'" + InstrumentsDb::toEscapedPath(Instr) + "'";
208      NewName = "'" + NewName + "'";      NewName = "'" + InstrumentsDb::toEscapedPath(NewName) + "'";
209      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));      LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instr_info, "NAME", Instr, NewName));
210  }  }
211    
212    void LSCPServer::DbInstrumentsEventHandler::JobStatusChanged(int JobId) {
213        LSCPServer::SendLSCPNotify(LSCPEvent(LSCPEvent::event_db_instrs_job_info, JobId));
214    }
215  #endif // HAVE_SQLITE3  #endif // HAVE_SQLITE3
216    
217    
# Line 212  int LSCPServer::Main() { Line 255  int LSCPServer::Main() {
255    
256      listen(hSocket, 1);      listen(hSocket, 1);
257      Initialized.Set(true);      Initialized.Set(true);
258        
259      // Registering event listeners      // Registering event listeners
260      pSampler->AddChannelCountListener(&eventHandler);      pSampler->AddChannelCountListener(&eventHandler);
261      pSampler->AddAudioDeviceCountListener(&eventHandler);      pSampler->AddAudioDeviceCountListener(&eventHandler);
# Line 321  int LSCPServer::Main() { Line 364  int LSCPServer::Main() {
364                                  int dummy; // just a temporary hack to fulfill the restart() function prototype                                  int dummy; // just a temporary hack to fulfill the restart() function prototype
365                                  restart(NULL, dummy); // restart the 'scanner'                                  restart(NULL, dummy); // restart the 'scanner'
366                                  currentSocket = (*iter).hSession;  //a hack                                  currentSocket = (*iter).hSession;  //a hack
367                                    itCurrentSession = iter; // another hack
368                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));                                  dmsg(2,("LSCPServer: [%s]\n",bufferedCommands[currentSocket].c_str()));
369                                  if ((*iter).bVerbose) { // if echo mode enabled                                  if ((*iter).bVerbose) { // if echo mode enabled
370                                      AnswerClient(bufferedCommands[currentSocket]);                                      AnswerClient(bufferedCommands[currentSocket]);
371                                  }                                  }
372                                  int result = yyparse(&(*iter));                                  int result = yyparse(&(*iter));
373                                  currentSocket = -1;     //continuation of a hack                                  currentSocket = -1;     //continuation of a hack
374                                    itCurrentSession = Sessions.end(); // hack as well
375                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));                                  dmsg(3,("LSCPServer: Done parsing on socket %d.\n", currentSocket));
376                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?                                  if (result == LSCP_QUIT) { //Was it a quit command by any chance?
377                                          CloseConnection(iter);                                          CloseConnection(iter);
# Line 419  extern int GetLSCPCommand( void *buf, in Line 464  extern int GetLSCPCommand( void *buf, in
464          return command.size();          return command.size();
465  }  }
466    
467    extern yyparse_param_t* GetCurrentYaccSession() {
468        return &(*itCurrentSession);
469    }
470    
471  /**  /**
472   * Will be called to try to read the command from the socket   * Will be called to try to read the command from the socket
473   * If command is read, it will return true. Otherwise false is returned.   * If command is read, it will return true. Otherwise false is returned.
# Line 606  EngineChannel* LSCPServer::GetEngineChan Line 655  EngineChannel* LSCPServer::GetEngineChan
655      EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();      EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
656      if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");      if (!pEngineChannel) throw Exception("There is no engine deployed on this sampler channel yet");
657    
658      return pEngineChannel;              return pEngineChannel;
659  }  }
660    
661  /**  /**
# Line 755  String LSCPServer::GetEngineInfo(String Line 804  String LSCPServer::GetEngineInfo(String
804      LockRTNotify();      LockRTNotify();
805      try {      try {
806          Engine* pEngine = EngineFactory::Create(EngineName);          Engine* pEngine = EngineFactory::Create(EngineName);
807          result.Add("DESCRIPTION", pEngine->Description());          result.Add("DESCRIPTION", _escapeLscpResponse(pEngine->Description()));
808          result.Add("VERSION",     pEngine->Version());          result.Add("VERSION",     pEngine->Version());
809          EngineFactory::Destroy(pEngine);          EngineFactory::Destroy(pEngine);
810      }      }
# Line 828  String LSCPServer::GetChannelInfo(uint u Line 877  String LSCPServer::GetChannelInfo(uint u
877          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");          if (pSamplerChannel->GetMidiInputChannel() == midi_chan_all) result.Add("MIDI_INPUT_CHANNEL", "ALL");
878          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());          else result.Add("MIDI_INPUT_CHANNEL", pSamplerChannel->GetMidiInputChannel());
879    
880          result.Add("INSTRUMENT_FILE", InstrumentFileName);          result.Add("INSTRUMENT_FILE",
881                       (InstrumentFileName != "NONE" && InstrumentFileName != "") ?
882                            Path::fromPosix(InstrumentFileName).toLscp() : // TODO: assuming POSIX
883                            InstrumentFileName
884            );
885          result.Add("INSTRUMENT_NR", InstrumentIndex);          result.Add("INSTRUMENT_NR", InstrumentIndex);
886          result.Add("INSTRUMENT_NAME", InstrumentName);          result.Add("INSTRUMENT_NAME", _escapeLscpResponse(InstrumentName));
887          result.Add("INSTRUMENT_STATUS", InstrumentStatus);          result.Add("INSTRUMENT_STATUS", InstrumentStatus);
888          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));          result.Add("MUTE", Mute == -1 ? "MUTED_BY_SOLO" : (Mute ? "true" : "false"));
889          result.Add("SOLO", Solo);          result.Add("SOLO", Solo);
# Line 1772  String LSCPServer::GetMidiInstrumentMapp Line 1825  String LSCPServer::GetMidiInstrumentMapp
1825          std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx);          std::map<midi_prog_index_t,MidiInstrumentMapper::entry_t>::iterator iter = mappings.find(idx);
1826          if (iter == mappings.end()) result.Error("there is no map entry with that index");          if (iter == mappings.end()) result.Error("there is no map entry with that index");
1827          else { // found          else { // found
1828              result.Add("NAME", iter->second.Name);              result.Add("NAME", _escapeLscpResponse(iter->second.Name));
1829              result.Add("ENGINE_NAME", iter->second.EngineName);              result.Add("ENGINE_NAME", iter->second.EngineName);
1830              result.Add("INSTRUMENT_FILE", iter->second.InstrumentFile);              result.Add("INSTRUMENT_FILE", Path::fromPosix(iter->second.InstrumentFile).toLscp()); //TODO: assuming POSIX
1831              result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);              result.Add("INSTRUMENT_NR", (int) iter->second.InstrumentIndex);
1832              String instrumentName;              String instrumentName;
1833              Engine* pEngine = EngineFactory::Create(iter->second.EngineName);              Engine* pEngine = EngineFactory::Create(iter->second.EngineName);
# Line 1787  String LSCPServer::GetMidiInstrumentMapp Line 1840  String LSCPServer::GetMidiInstrumentMapp
1840                  }                  }
1841                  EngineFactory::Destroy(pEngine);                  EngineFactory::Destroy(pEngine);
1842              }              }
1843              result.Add("INSTRUMENT_NAME", instrumentName);              result.Add("INSTRUMENT_NAME", _escapeLscpResponse(instrumentName));
1844              switch (iter->second.LoadMode) {              switch (iter->second.LoadMode) {
1845                  case MidiInstrumentMapper::ON_DEMAND:                  case MidiInstrumentMapper::ON_DEMAND:
1846                      result.Add("LOAD_MODE", "ON_DEMAND");                      result.Add("LOAD_MODE", "ON_DEMAND");
# Line 1942  String LSCPServer::GetMidiInstrumentMap( Line 1995  String LSCPServer::GetMidiInstrumentMap(
1995      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));      dmsg(2,("LSCPServer: GetMidiInstrumentMap()\n"));
1996      LSCPResultSet result;      LSCPResultSet result;
1997      try {      try {
1998          result.Add("NAME", MidiInstrumentMapper::MapName(MidiMapID));          result.Add("NAME", _escapeLscpResponse(MidiInstrumentMapper::MapName(MidiMapID)));
1999          result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);          result.Add("DEFAULT", MidiInstrumentMapper::GetDefaultMap() == MidiMapID);
2000      } catch (Exception e) {      } catch (Exception e) {
2001          result.Error(e);          result.Error(e);
# Line 1993  String LSCPServer::CreateFxSend(uint uiS Line 2046  String LSCPServer::CreateFxSend(uint uiS
2046      LSCPResultSet result;      LSCPResultSet result;
2047      try {      try {
2048          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2049            
2050          FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);          FxSend* pFxSend = pEngineChannel->AddFxSend(MidiCtrl, Name);
2051          if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");          if (!pFxSend) throw Exception("Could not add FxSend, don't ask, I don't know why (probably a bug)");
2052    
# Line 2077  String LSCPServer::GetFxSendInfo(uint ui Line 2130  String LSCPServer::GetFxSendInfo(uint ui
2130      try {      try {
2131          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);          EngineChannel* pEngineChannel = GetEngineChannel(uiSamplerChannel);
2132          FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);          FxSend* pFxSend = GetFxSend(uiSamplerChannel, FxSendID);
2133            
2134          // gather audio routing informations          // gather audio routing informations
2135          String AudioRouting;          String AudioRouting;
2136          for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {          for (int chan = 0; chan < pEngineChannel->Channels(); chan++) {
# Line 2086  String LSCPServer::GetFxSendInfo(uint ui Line 2139  String LSCPServer::GetFxSendInfo(uint ui
2139          }          }
2140    
2141          // success          // success
2142          result.Add("NAME", pFxSend->Name());          result.Add("NAME", _escapeLscpResponse(pFxSend->Name()));
2143          result.Add("MIDI_CONTROLLER", pFxSend->MidiController());          result.Add("MIDI_CONTROLLER", pFxSend->MidiController());
2144          result.Add("LEVEL", ToString(pFxSend->Level()));          result.Add("LEVEL", ToString(pFxSend->Level()));
2145          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);          result.Add("AUDIO_OUTPUT_ROUTING", AudioRouting);
# Line 2152  String LSCPServer::SetFxSendLevel(uint u Line 2205  String LSCPServer::SetFxSendLevel(uint u
2205      return result.Produce();      return result.Produce();
2206  }  }
2207    
2208    String LSCPServer::EditSamplerChannelInstrument(uint uiSamplerChannel) {
2209        dmsg(2,("LSCPServer: EditSamplerChannelInstrument(SamplerChannel=%d)\n", uiSamplerChannel));
2210        LSCPResultSet result;
2211        try {
2212            SamplerChannel* pSamplerChannel = pSampler->GetSamplerChannel(uiSamplerChannel);
2213            if (!pSamplerChannel) throw Exception("Invalid sampler channel number " + ToString(uiSamplerChannel));
2214            EngineChannel* pEngineChannel = pSamplerChannel->GetEngineChannel();
2215            if (!pEngineChannel) throw Exception("No engine type assigned to sampler channel");
2216            if (pEngineChannel->InstrumentStatus() < 0) throw Exception("No instrument loaded to sampler channel");
2217            Engine* pEngine = pEngineChannel->GetEngine();
2218            InstrumentManager* pInstrumentManager = pEngine->GetInstrumentManager();
2219            if (!pInstrumentManager) throw Exception("Engine does not provide an instrument manager");
2220            InstrumentManager::instrument_id_t instrumentID;
2221            instrumentID.FileName = pEngineChannel->InstrumentFileName();
2222            instrumentID.Index    = pEngineChannel->InstrumentIndex();
2223            pInstrumentManager->LaunchInstrumentEditor(instrumentID);
2224        } catch (Exception e) {
2225            result.Error(e);
2226        }
2227        return result.Produce();
2228    }
2229    
2230  /**  /**
2231   * Will be called by the parser to reset a particular sampler channel.   * Will be called by the parser to reset a particular sampler channel.
2232   */   */
# Line 2187  String LSCPServer::ResetSampler() { Line 2262  String LSCPServer::ResetSampler() {
2262   */   */
2263  String LSCPServer::GetServerInfo() {  String LSCPServer::GetServerInfo() {
2264      dmsg(2,("LSCPServer: GetServerInfo()\n"));      dmsg(2,("LSCPServer: GetServerInfo()\n"));
2265        const std::string description =
2266            _escapeLscpResponse("LinuxSampler - modular, streaming capable sampler");
2267      LSCPResultSet result;      LSCPResultSet result;
2268      result.Add("DESCRIPTION", "LinuxSampler - modular, streaming capable sampler");      result.Add("DESCRIPTION", description);
2269      result.Add("VERSION", VERSION);      result.Add("VERSION", VERSION);
2270      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));      result.Add("PROTOCOL_VERSION", ToString(LSCP_RELEASE_MAJOR) + "." + ToString(LSCP_RELEASE_MINOR));
2271  #if HAVE_SQLITE3  #if HAVE_SQLITE3
# Line 2196  String LSCPServer::GetServerInfo() { Line 2273  String LSCPServer::GetServerInfo() {
2273  #else  #else
2274      result.Add("INSTRUMENTS_DB_SUPPORT", "no");      result.Add("INSTRUMENTS_DB_SUPPORT", "no");
2275  #endif  #endif
2276        
2277      return result.Produce();      return result.Produce();
2278  }  }
2279    
# Line 2294  String LSCPServer::RemoveDbInstrumentDir Line 2371  String LSCPServer::RemoveDbInstrumentDir
2371      return result.Produce();      return result.Produce();
2372  }  }
2373    
2374  String LSCPServer::GetDbInstrumentDirectoryCount(String Dir) {  String LSCPServer::GetDbInstrumentDirectoryCount(String Dir, bool Recursive) {
2375      dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s)\n", Dir.c_str()));      dmsg(2,("LSCPServer: GetDbInstrumentDirectoryCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2376      LSCPResultSet result;      LSCPResultSet result;
2377  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2378      try {      try {
2379          result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir));          result.Add(InstrumentsDb::GetInstrumentsDb()->GetDirectoryCount(Dir, Recursive));
2380      } catch (Exception e) {      } catch (Exception e) {
2381           result.Error(e);           result.Error(e);
2382      }      }
# Line 2309  String LSCPServer::GetDbInstrumentDirect Line 2386  String LSCPServer::GetDbInstrumentDirect
2386      return result.Produce();      return result.Produce();
2387  }  }
2388    
2389  String LSCPServer::GetDbInstrumentDirectories(String Dir) {  String LSCPServer::GetDbInstrumentDirectories(String Dir, bool Recursive) {
2390      dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));      dmsg(2,("LSCPServer: GetDbInstrumentDirectories(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2391      LSCPResultSet result;      LSCPResultSet result;
2392  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2393      try {      try {
2394          String list;          String list;
2395          StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir);          StringListPtr dirs = InstrumentsDb::GetInstrumentsDb()->GetDirectories(Dir, Recursive);
2396    
2397          for (int i = 0; i < dirs->size(); i++) {          for (int i = 0; i < dirs->size(); i++) {
2398              if (list != "") list += ",";              if (list != "") list += ",";
2399              list += "'" + dirs->at(i) + "'";              list += "'" + InstrumentsDb::toEscapedPath(dirs->at(i)) + "'";
2400          }          }
2401    
2402          result.Add(list);          result.Add(list);
# Line 2339  String LSCPServer::GetDbInstrumentDirect Line 2416  String LSCPServer::GetDbInstrumentDirect
2416      try {      try {
2417          DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);          DbDirectory info = InstrumentsDb::GetInstrumentsDb()->GetDirectoryInfo(Dir);
2418    
2419          result.Add("DESCRIPTION", info.Description);          result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2420          result.Add("CREATED", info.Created);          result.Add("CREATED", info.Created);
2421          result.Add("MODIFIED", info.Modified);          result.Add("MODIFIED", info.Modified);
2422      } catch (Exception e) {      } catch (Exception e) {
# Line 2381  String LSCPServer::MoveDbInstrumentDirec Line 2458  String LSCPServer::MoveDbInstrumentDirec
2458      return result.Produce();      return result.Produce();
2459  }  }
2460    
2461  String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {  String LSCPServer::CopyDbInstrumentDirectory(String Dir, String Dst) {
2462      dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));      dmsg(2,("LSCPServer: CopyDbInstrumentDirectory(Dir=%s,Dst=%s)\n", Dir.c_str(), Dst.c_str()));
2463      LSCPResultSet result;      LSCPResultSet result;
2464  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2465      try {      try {
2466          InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);          InstrumentsDb::GetInstrumentsDb()->CopyDirectory(Dir, Dst);
2467      } catch (Exception e) {      } catch (Exception e) {
2468           result.Error(e);           result.Error(e);
2469      }      }
# Line 2396  String LSCPServer::SetDbInstrumentDirect Line 2473  String LSCPServer::SetDbInstrumentDirect
2473      return result.Produce();      return result.Produce();
2474  }  }
2475    
2476  String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index) {  String LSCPServer::SetDbInstrumentDirectoryDescription(String Dir, String Desc) {
2477      dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d)\n", DbDir.c_str(), FilePath.c_str(), Index));      dmsg(2,("LSCPServer: SetDbInstrumentDirectoryDescription(Dir=%s,Desc=%s)\n", Dir.c_str(), Desc.c_str()));
2478      LSCPResultSet result;      LSCPResultSet result;
2479  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2480      try {      try {
2481          InstrumentsDb::GetInstrumentsDb()->AddInstruments(DbDir, FilePath, Index);          InstrumentsDb::GetInstrumentsDb()->SetDirectoryDescription(Dir, Desc);
2482      } catch (Exception e) {      } catch (Exception e) {
2483           result.Error(e);           result.Error(e);
2484      }      }
# Line 2411  String LSCPServer::AddDbInstruments(Stri Line 2488  String LSCPServer::AddDbInstruments(Stri
2488      return result.Produce();      return result.Produce();
2489  }  }
2490    
2491  String LSCPServer::AddDbInstrumentsFlat(String DbDir, String FsDir) {  String LSCPServer::AddDbInstruments(String DbDir, String FilePath, int Index, bool bBackground) {
2492      dmsg(2,("LSCPServer: AddDbInstrumentsFlat(DbDir=%s,FilePath=%s)\n", DbDir.c_str(), FsDir.c_str()));      dmsg(2,("LSCPServer: AddDbInstruments(DbDir=%s,FilePath=%s,Index=%d,bBackground=%d)\n", DbDir.c_str(), FilePath.c_str(), Index, bBackground));
2493      LSCPResultSet result;      LSCPResultSet result;
2494  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2495      try {      try {
2496          InstrumentsDb::GetInstrumentsDb()->AddInstrumentsRecursive(DbDir, FsDir, true);          int id;
2497            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2498            id = db->AddInstruments(DbDir, FilePath, Index, bBackground);
2499            if (bBackground) result = id;
2500      } catch (Exception e) {      } catch (Exception e) {
2501           result.Error(e);           result.Error(e);
2502      }      }
# Line 2426  String LSCPServer::AddDbInstrumentsFlat( Line 2506  String LSCPServer::AddDbInstrumentsFlat(
2506      return result.Produce();      return result.Produce();
2507  }  }
2508    
2509  String LSCPServer::AddDbInstrumentsNonrecursive(String DbDir, String FsDir) {  String LSCPServer::AddDbInstruments(String ScanMode, String DbDir, String FsDir, bool bBackground) {
2510      dmsg(2,("LSCPServer: AddDbInstrumentsNonrecursive(DbDir=%s,FilePath=%s)\n", DbDir.c_str(), FsDir.c_str()));      dmsg(2,("LSCPServer: AddDbInstruments(ScanMode=%s,DbDir=%s,FsDir=%s,bBackground=%d)\n", ScanMode.c_str(), DbDir.c_str(), FsDir.c_str(), bBackground));
2511      LSCPResultSet result;      LSCPResultSet result;
2512  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2513      try {      try {
2514          InstrumentsDb::GetInstrumentsDb()->AddInstrumentsNonrecursive(DbDir, FsDir);          int id;
2515            InstrumentsDb* db = InstrumentsDb::GetInstrumentsDb();
2516            if (ScanMode.compare("RECURSIVE") == 0) {
2517               id = db->AddInstruments(RECURSIVE, DbDir, FsDir, bBackground);
2518            } else if (ScanMode.compare("NON_RECURSIVE") == 0) {
2519               id = db->AddInstruments(NON_RECURSIVE, DbDir, FsDir, bBackground);
2520            } else if (ScanMode.compare("FLAT") == 0) {
2521               id = db->AddInstruments(FLAT, DbDir, FsDir, bBackground);
2522            } else {
2523                throw Exception("Unknown scan mode: " + ScanMode);
2524            }
2525    
2526            if (bBackground) result = id;
2527      } catch (Exception e) {      } catch (Exception e) {
2528           result.Error(e);           result.Error(e);
2529      }      }
# Line 2456  String LSCPServer::RemoveDbInstrument(St Line 2548  String LSCPServer::RemoveDbInstrument(St
2548      return result.Produce();      return result.Produce();
2549  }  }
2550    
2551  String LSCPServer::GetDbInstrumentCount(String Dir) {  String LSCPServer::GetDbInstrumentCount(String Dir, bool Recursive) {
2552      dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s)\n", Dir.c_str()));      dmsg(2,("LSCPServer: GetDbInstrumentCount(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2553      LSCPResultSet result;      LSCPResultSet result;
2554  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2555      try {      try {
2556          result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir));          result.Add(InstrumentsDb::GetInstrumentsDb()->GetInstrumentCount(Dir, Recursive));
2557      } catch (Exception e) {      } catch (Exception e) {
2558           result.Error(e);           result.Error(e);
2559      }      }
# Line 2471  String LSCPServer::GetDbInstrumentCount( Line 2563  String LSCPServer::GetDbInstrumentCount(
2563      return result.Produce();      return result.Produce();
2564  }  }
2565    
2566  String LSCPServer::GetDbInstruments(String Dir) {  String LSCPServer::GetDbInstruments(String Dir, bool Recursive) {
2567      dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s)\n", Dir.c_str()));      dmsg(2,("LSCPServer: GetDbInstruments(Dir=%s,Recursive=%d)\n", Dir.c_str(), Recursive));
2568      LSCPResultSet result;      LSCPResultSet result;
2569  #if HAVE_SQLITE3  #if HAVE_SQLITE3
2570      try {      try {
2571          String list;          String list;
2572          StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir);          StringListPtr instrs = InstrumentsDb::GetInstrumentsDb()->GetInstruments(Dir, Recursive);
2573    
2574          for (int i = 0; i < instrs->size(); i++) {          for (int i = 0; i < instrs->size(); i++) {
2575              if (list != "") list += ",";              if (list != "") list += ",";
2576              list += "'" + instrs->at(i) + "'";              list += "'" + InstrumentsDb::toEscapedPath(instrs->at(i)) + "'";
2577          }          }
2578    
2579          result.Add(list);          result.Add(list);
# Line 2508  String LSCPServer::GetDbInstrumentInfo(S Line 2600  String LSCPServer::GetDbInstrumentInfo(S
2600          result.Add("SIZE", (int)info.Size);          result.Add("SIZE", (int)info.Size);
2601          result.Add("CREATED", info.Created);          result.Add("CREATED", info.Created);
2602          result.Add("MODIFIED", info.Modified);          result.Add("MODIFIED", info.Modified);
2603          result.Add("DESCRIPTION", FilterEndlines(info.Description));          result.Add("DESCRIPTION", _escapeLscpResponse(info.Description));
2604          result.Add("IS_DRUM", info.IsDrum);          result.Add("IS_DRUM", info.IsDrum);
2605          result.Add("PRODUCT", FilterEndlines(info.Product));          result.Add("PRODUCT", _escapeLscpResponse(info.Product));
2606          result.Add("ARTISTS", FilterEndlines(info.Artists));          result.Add("ARTISTS", _escapeLscpResponse(info.Artists));
2607          result.Add("KEYWORDS", FilterEndlines(info.Keywords));          result.Add("KEYWORDS", _escapeLscpResponse(info.Keywords));
2608        } catch (Exception e) {
2609             result.Error(e);
2610        }
2611    #else
2612        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2613    #endif
2614        return result.Produce();
2615    }
2616    
2617    String LSCPServer::GetDbInstrumentsJobInfo(int JobId) {
2618        dmsg(2,("LSCPServer: GetDbInstrumentsJobInfo(JobId=%d)\n", JobId));
2619        LSCPResultSet result;
2620    #if HAVE_SQLITE3
2621        try {
2622            ScanJob job = InstrumentsDb::GetInstrumentsDb()->Jobs.GetJobById(JobId);
2623    
2624            result.Add("FILES_TOTAL", job.FilesTotal);
2625            result.Add("FILES_SCANNED", job.FilesScanned);
2626            result.Add("SCANNING", job.Scanning);
2627            result.Add("STATUS", job.Status);
2628      } catch (Exception e) {      } catch (Exception e) {
2629           result.Error(e);           result.Error(e);
2630      }      }
# Line 2552  String LSCPServer::MoveDbInstrument(Stri Line 2664  String LSCPServer::MoveDbInstrument(Stri
2664      return result.Produce();      return result.Produce();
2665  }  }
2666    
2667    String LSCPServer::CopyDbInstrument(String Instr, String Dst) {
2668        dmsg(2,("LSCPServer: CopyDbInstrument(Instr=%s,Dst=%s)\n", Instr.c_str(), Dst.c_str()));
2669        LSCPResultSet result;
2670    #if HAVE_SQLITE3
2671        try {
2672            InstrumentsDb::GetInstrumentsDb()->CopyInstrument(Instr, Dst);
2673        } catch (Exception e) {
2674             result.Error(e);
2675        }
2676    #else
2677        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2678    #endif
2679        return result.Produce();
2680    }
2681    
2682  String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {  String LSCPServer::SetDbInstrumentDescription(String Instr, String Desc) {
2683      dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));      dmsg(2,("LSCPServer: SetDbInstrumentDescription(Instr=%s,Desc=%s)\n", Instr.c_str(), Desc.c_str()));
2684      LSCPResultSet result;      LSCPResultSet result;
# Line 2567  String LSCPServer::SetDbInstrumentDescri Line 2694  String LSCPServer::SetDbInstrumentDescri
2694      return result.Produce();      return result.Produce();
2695  }  }
2696    
2697    String LSCPServer::FindDbInstrumentDirectories(String Dir, std::map<String,String> Parameters, bool Recursive) {
2698        dmsg(2,("LSCPServer: FindDbInstrumentDirectories(Dir=%s)\n", Dir.c_str()));
2699        LSCPResultSet result;
2700    #if HAVE_SQLITE3
2701        try {
2702            SearchQuery Query;
2703            std::map<String,String>::iterator iter;
2704            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
2705                if (iter->first.compare("NAME") == 0) {
2706                    Query.Name = iter->second;
2707                } else if (iter->first.compare("CREATED") == 0) {
2708                    Query.SetCreated(iter->second);
2709                } else if (iter->first.compare("MODIFIED") == 0) {
2710                    Query.SetModified(iter->second);
2711                } else if (iter->first.compare("DESCRIPTION") == 0) {
2712                    Query.Description = iter->second;
2713                } else {
2714                    throw Exception("Unknown search criteria: " + iter->first);
2715                }
2716            }
2717    
2718            String list;
2719            StringListPtr pDirectories =
2720                InstrumentsDb::GetInstrumentsDb()->FindDirectories(Dir, &Query, Recursive);
2721    
2722            for (int i = 0; i < pDirectories->size(); i++) {
2723                if (list != "") list += ",";
2724                list += "'" + InstrumentsDb::toEscapedPath(pDirectories->at(i)) + "'";
2725            }
2726    
2727            result.Add(list);
2728        } catch (Exception e) {
2729             result.Error(e);
2730        }
2731    #else
2732        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2733    #endif
2734        return result.Produce();
2735    }
2736    
2737    String LSCPServer::FindDbInstruments(String Dir, std::map<String,String> Parameters, bool Recursive) {
2738        dmsg(2,("LSCPServer: FindDbInstruments(Dir=%s)\n", Dir.c_str()));
2739        LSCPResultSet result;
2740    #if HAVE_SQLITE3
2741        try {
2742            SearchQuery Query;
2743            std::map<String,String>::iterator iter;
2744            for (iter = Parameters.begin(); iter != Parameters.end(); iter++) {
2745                if (iter->first.compare("NAME") == 0) {
2746                    Query.Name = iter->second;
2747                } else if (iter->first.compare("FORMAT_FAMILIES") == 0) {
2748                    Query.SetFormatFamilies(iter->second);
2749                } else if (iter->first.compare("SIZE") == 0) {
2750                    Query.SetSize(iter->second);
2751                } else if (iter->first.compare("CREATED") == 0) {
2752                    Query.SetCreated(iter->second);
2753                } else if (iter->first.compare("MODIFIED") == 0) {
2754                    Query.SetModified(iter->second);
2755                } else if (iter->first.compare("DESCRIPTION") == 0) {
2756                    Query.Description = iter->second;
2757                } else if (iter->first.compare("IS_DRUM") == 0) {
2758                    if (!strcasecmp(iter->second.c_str(), "true")) {
2759                        Query.InstrType = SearchQuery::DRUM;
2760                    } else {
2761                        Query.InstrType = SearchQuery::CHROMATIC;
2762                    }
2763                } else if (iter->first.compare("PRODUCT") == 0) {
2764                     Query.Product = iter->second;
2765                } else if (iter->first.compare("ARTISTS") == 0) {
2766                     Query.Artists = iter->second;
2767                } else if (iter->first.compare("KEYWORDS") == 0) {
2768                     Query.Keywords = iter->second;
2769                } else {
2770                    throw Exception("Unknown search criteria: " + iter->first);
2771                }
2772            }
2773    
2774            String list;
2775            StringListPtr pInstruments =
2776                InstrumentsDb::GetInstrumentsDb()->FindInstruments(Dir, &Query, Recursive);
2777    
2778            for (int i = 0; i < pInstruments->size(); i++) {
2779                if (list != "") list += ",";
2780                list += "'" + InstrumentsDb::toEscapedPath(pInstruments->at(i)) + "'";
2781            }
2782    
2783            result.Add(list);
2784        } catch (Exception e) {
2785             result.Error(e);
2786        }
2787    #else
2788        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2789    #endif
2790        return result.Produce();
2791    }
2792    
2793    String LSCPServer::FormatInstrumentsDb() {
2794        dmsg(2,("LSCPServer: FormatInstrumentsDb()\n"));
2795        LSCPResultSet result;
2796    #if HAVE_SQLITE3
2797        try {
2798            InstrumentsDb::GetInstrumentsDb()->Format();
2799        } catch (Exception e) {
2800             result.Error(e);
2801        }
2802    #else
2803        result.Error(String(DOESNT_HAVE_SQLITE3), 0);
2804    #endif
2805        return result.Produce();
2806    }
2807    
2808    
2809  /**  /**
2810   * Will be called by the parser to enable or disable echo mode; if echo   * Will be called by the parser to enable or disable echo mode; if echo
# Line 2586  String LSCPServer::SetEcho(yyparse_param Line 2824  String LSCPServer::SetEcho(yyparse_param
2824      }      }
2825      return result.Produce();      return result.Produce();
2826  }  }
   
 String LSCPServer::FilterEndlines(String s) {  
     String s2 = s;  
     for (int i = 0; i < s2.length(); i++) {  
         if (s2.at(i) == '\r') s2.at(i) = ' ';  
         else if (s2.at(i) == '\n') s2.at(i) = ' ';  
     }  
       
     return s2;  
 }  

Legend:
Removed from v.1161  
changed lines
  Added in v.1403

  ViewVC Help
Powered by ViewVC