void QSpotifySession::checkNetworkAccess()
{
  //  qDebug() << "QSpotifySession::checkNetworkAccess";
    if (!m_networkConfManager->isOnline()) {
        sp_session_set_connection_type(m_sp_session, SP_CONNECTION_TYPE_NONE);
        setOfflineMode(true, true);
    } else {
        bool wifi = false;
        bool mobile = false;
        bool roaming = false;
        QList<QNetworkConfiguration> confs = m_networkConfManager->allConfigurations(QNetworkConfiguration::Active);
        for (int i = 0; i < confs.count(); ++i) {
            QNetworkConfiguration::BearerType bearer = confs.at(i).bearerType();
            qDebug() << "Network connection type: " << confs.at(i).bearerTypeName();
            if (bearer == QNetworkConfiguration::BearerWLAN || bearer == QNetworkConfiguration::BearerEthernet) {
                wifi = true;
                break;
            } else {
                mobile = true;
            }
            if (confs.at(i).isRoamingAvailable()) {
                roaming = true;
            }
        }

        sp_connection_type type;
        if (wifi)
            type = SP_CONNECTION_TYPE_WIFI;
        else if (roaming)
            type = SP_CONNECTION_TYPE_MOBILE_ROAMING;
        else if (mobile)
            type = SP_CONNECTION_TYPE_MOBILE;
        else
            type = SP_CONNECTION_TYPE_UNKNOWN;

        sp_session_set_connection_type(m_sp_session, type);

        if (m_forcedOfflineMode)
            setOfflineMode(false, true);
        else
            setConnectionRules(m_offlineMode ? m_connectionRules & ~AllowNetwork :
                                               m_connectionRules | AllowNetwork);
    }
}
示例#2
0
  bool Session::connect() {
    if (!m_session) {
      sp_session_config config;
      Logger::printOut("Creating session");
	 
      config.api_version = SPOTIFY_API_VERSION;
      Logger::printOut("API version:");
      char* version = new char[20];
      Logger::printOut(itoa(SPOTIFY_API_VERSION, version, 10));
	  
	  //the api is not copying the string so create a new c string
	  CStdString location = Settings::getCachePath();
	  char * cstr;
      cstr = new char [location.size()+1];
      strcpy (cstr, location.c_str());
      config.cache_location = cstr;
      config.settings_location = cstr;
      config.tracefile = NULL;

      config.application_key = g_appkey;
      config.application_key_size = g_appkey_size;
      config.user_agent = "spotyXBMC2";
      config.device_id = "XBMC htpc";

      sp_session_callbacks cb = m_sessionCallbacks.getCallbacks();
      config.callbacks = &cb;
      config.compress_playlists = true;
      config.dont_save_metadata_for_playlists = false;
      config.initially_unload_playlists = false;

      sp_error error = sp_session_create(&config, &m_session);

      if (SP_ERROR_OK != error) {
        Logger::printOut("Failed to create session: error:");
        Logger::printOut(sp_error_message(error));
        m_session = NULL;
        return false;
      }

      //set high bitrate
      if (Settings::useHighBitrate()) sp_session_preferred_bitrate(m_session, SP_BITRATE_320k);

      sp_session_set_connection_type(m_session, SP_CONNECTION_TYPE_WIRED);
      sp_session_set_connection_rules(m_session, SP_CONNECTION_RULE_NETWORK);
      sp_session_set_volume_normalization(m_session, Settings::useNormalization());

      sp_session_login(m_session, Settings::getUserName().c_str(), Settings::getPassword().c_str(), true);
      m_isEnabled = true;
      Logger::printOut("Logged in, returning");
      return true;
    }
    return false;
  }
示例#3
0
void QSpotifySession::checkNetworkAccess()
{
    qDebug() << "QSpotifySession::checkNetworkAccess";
    if (!isOnline()) {
        sp_session_set_connection_type(m_sp_session, SP_CONNECTION_TYPE_NONE);
        setOfflineMode(true, true);
    } else {
        bool limited = m_networkingStatus->limitations().contains(ubuntu::connectivity::NetworkingStatus::Limitations::Bandwith);
        sp_connection_type type;
        if (!limited)
            type = SP_CONNECTION_TYPE_WIFI;
        else
            type = SP_CONNECTION_TYPE_MOBILE;

        sp_session_set_connection_type(m_sp_session, type);

        if (m_forcedOfflineMode)
            setOfflineMode(false, true);
        else
            setConnectionRules(m_offlineMode ? m_connectionRules & ~AllowNetwork :
                                               m_connectionRules | AllowNetwork);
    }
}
示例#4
0
int main(int argc, char **argv) {
	int listen_fd;
	sp_session *session;
	static sp_session_config config;
	static sp_session_callbacks callbacks = {
		.logged_in		= &sess_callback_logged_in,
		.logged_out		= &sess_callback_logged_out,
		.metadata_updated	= &sess_callback_metadata_updated,
		.connection_error	= NULL,
		.message_to_user	= &sess_callback_message_to_user,
		.notify_main_thread	= &sess_callback_notify,
		.music_delivery		= &player_callback_frame_delivery,
		.play_token_lost	= &player_callback_playtoken_lost,
		.log_message		= &sess_callback_log_message,
		.end_of_track		= &player_callback_end_of_track,
		.streaming_error	= NULL,
		.userinfo_updated	= NULL,
		.start_playback		= &player_callback_start_playback,
		.stop_playback		= &player_callback_stop_playback,
		.get_audio_buffer_stats	= &player_callback_get_audio_buffer_stats,
		// libspotify 10
		.offline_status_updated	= &sess_callback_offline_status_updated,
		.offline_error		= &sess_callback_offline_error,
		// libspotify 11
		.credentials_blob_updated	= sess_callback_credentials_blob_updated,
		// libspotify 12
		.connectionstate_updated	= &sess_callback_connectionstate_updated,
		.scrobble_error			= NULL,
		.private_session_mode_changed	= NULL,
	};

	thread_main = pthread_self();

	/* Setup logging to stderr */
	openlog(LIBSPOTIFY_USERAGENT, LOG_PERROR, LOG_USER);

	/**
	 * Filter logging with one of these 
	 *
	setlogmask(LOG_UPTO(LOG_WARNING));
	setlogmask(LOG_UPTO(LOG_NOTICE));
	setlogmask(LOG_UPTO(LOG_DEBUG));
	 */
	setlogmask(LOG_UPTO(LOG_INFO));

	config.api_version = SPOTIFY_API_VERSION;
	config.cache_location = LIBSPOTIFY_CACHE_DIR;
	config.settings_location = LIBSPOTIFY_CACHE_DIR;
	config.application_key = g_appkey;
	config.application_key_size = sizeof(g_appkey);
	config.user_agent = LIBSPOTIFY_USERAGENT;
	config.callbacks = &callbacks;
	config.userdata = app_create();
	config.compress_playlists = 1;
	config.dont_save_metadata_for_playlists = 0;
	config.initially_unload_playlists = 0;
	config.device_id = NULL;

	syslog(LOG_DEBUG, "MAIN: Initializing libspotify");
	if(sp_session_create(&config, &session) != SP_ERROR_OK) {
		syslog(LOG_ERR, "MAIN: Unable to initialize libspotify");
		app_release();
		return -1;
	}

	app_set_session(session);
	if(argc == 4) {
		sp_link *link = sp_link_create_from_string(argv[3]);
		app_set_link(link);
	}

	/* This program will be run on mobile internet connections */
	sp_session_set_connection_type(session, SP_CONNECTION_TYPE_MOBILE_ROAMING);
	sp_session_set_connection_rules(session, SP_CONNECTION_RULE_NETWORK|SP_CONNECTION_RULE_NETWORK_IF_ROAMING|SP_CONNECTION_RULE_ALLOW_SYNC_OVER_MOBILE);
	sp_session_preferred_offline_bitrate(session, SP_BITRATE_160k, 0);

	if(argc < 2) {
		char username[256];

		if(sp_session_remembered_user(session, username, sizeof(username)) > 0)
			syslog(LOG_DEBUG, "MAIN: Attempting to login using stored credentials for user '%s'", username);

		if(sp_session_relogin(session) == SP_ERROR_NO_CREDENTIALS) {
			syslog(LOG_ERR, "MAIN: No credentials stored. Please run: %s <username> <password>", argv[0]);
			app_release();
			return -1;
		}
	}
	else {
		syslog(LOG_DEBUG, "MAIN: Attempting to login using command line credentials");
		sp_session_login(session, argv[1], argc == 3? argv[2]: NULL, 1, get_auth_blob());
	}

	if((listen_fd = net_create(CTRL_TCP_PORT)) < 0) {
		syslog(LOG_ERR, "MAIN: Failed to initialize external network");
		app_release();
		return -1;
	}

	mainloop(session, listen_fd);
	syslog(LOG_INFO, "MAIN: Outside main event loop, good bye!");

	net_release(listen_fd);
	app_release();

	return 0;
}