/**
   * Saves the last status for all accounts. This information is used on loging. Each time user logs
   * in he's logged with the same status as he was the last time before closing the application.
   *
   * @param protocolProvider the protocol provider to save status information for
   * @param statusName the name of the status to save
   */
  private void saveStatusInformation(ProtocolProviderService protocolProvider, String statusName) {
    ConfigurationService configService = GuiActivator.getConfigurationService();

    String prefix = "net.java.sip.communicator.impl.gui.accounts";

    List<String> accounts = configService.getPropertyNamesByPrefix(prefix, true);

    boolean savedAccount = false;

    for (String accountRootPropName : accounts) {
      String accountUID = configService.getString(accountRootPropName);

      if (accountUID.equals(protocolProvider.getAccountID().getAccountUniqueID())) {

        configService.setProperty(accountRootPropName + ".lastAccountStatus", statusName);

        savedAccount = true;
      }
    }

    if (!savedAccount) {
      String accNodeName = "acc" + Long.toString(System.currentTimeMillis());

      String accountPackage = "net.java.sip.communicator.impl.gui.accounts." + accNodeName;

      configService.setProperty(
          accountPackage, protocolProvider.getAccountID().getAccountUniqueID());

      configService.setProperty(accountPackage + ".lastAccountStatus", statusName);
    }
  }
  /** Installs an account and verifies whether the installation has gone well. */
  public void testInstallAccount() {
    // first obtain a reference to the provider factory
    ServiceReference[] serRefs = null;
    String osgiFilter = "(" + ProtocolProviderFactory.PROTOCOL + "=" + ProtocolNames.JABBER + ")";
    try {
      serRefs =
          JabberSlickFixture.bc.getServiceReferences(
              ProtocolProviderFactory.class.getName(), osgiFilter);
    } catch (InvalidSyntaxException ex) {
      // this really shouldhn't occur as the filter expression is static.
      fail(osgiFilter + " is not a valid osgi filter");
    }

    assertTrue(
        "Failed to find a provider factory service for protocol Jabber",
        serRefs != null && serRefs.length > 0);

    // Enable always trust mode for testing tls jabber connections
    ServiceReference confReference =
        JabberSlickFixture.bc.getServiceReference(ConfigurationService.class.getName());
    ConfigurationService configurationService =
        (ConfigurationService) JabberSlickFixture.bc.getService(confReference);

    configurationService.setProperty(CertificateService.PNAME_ALWAYS_TRUST, Boolean.TRUE);

    // Keep the reference for later usage.
    ProtocolProviderFactory jabberProviderFactory =
        (ProtocolProviderFactory) JabberSlickFixture.bc.getService(serRefs[0]);

    // make sure the account is empty
    assertTrue(
        "There was an account registered with the account mananger " + "before we've installed any",
        jabberProviderFactory.getRegisteredAccounts().size() == 0);

    // Prepare the properties of the first jabber account.

    Hashtable<String, String> jabberAccount1Properties =
        getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_1_PREFIX);
    Hashtable<String, String> jabberAccount2Properties =
        getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_2_PREFIX);
    Hashtable<String, String> jabberAccount3Properties =
        getAccountProperties(JabberProtocolProviderServiceLick.ACCOUNT_3_PREFIX);

    // try to install an account with a null account id
    try {
      jabberProviderFactory.installAccount(null, jabberAccount1Properties);
      fail(
          "installing an account with a null account id must result "
              + "in a NullPointerException");
    } catch (NullPointerException exc) {
      // that's what had to happen
    }

    // now really install the accounts
    jabberProviderFactory.installAccount(
        jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount1Properties);
    jabberProviderFactory.installAccount(
        jabberAccount2Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount2Properties);
    jabberProviderFactory.installAccount(
        jabberAccount3Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount3Properties);

    // try to install one of the accounts one more time and verify that an
    // excepion is thrown.
    try {
      jabberProviderFactory.installAccount(
          jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID), jabberAccount1Properties);

      fail(
          "An IllegalStateException must be thrown when trying to "
              + "install a duplicate account");

    } catch (IllegalStateException exc) {
      // that's what supposed to happen.
    }

    // Verify that the provider factory is aware of our installation
    assertTrue(
        "The newly installed account was not in the acc man's " + "registered accounts!",
        jabberProviderFactory.getRegisteredAccounts().size() == 3);

    // Verify protocol providers corresponding to the new account have
    // been properly registered with the osgi framework.

    osgiFilter =
        "(&("
            + ProtocolProviderFactory.PROTOCOL
            + "="
            + ProtocolNames.JABBER
            + ")"
            + "("
            + ProtocolProviderFactory.USER_ID
            + "="
            + jabberAccount1Properties.get(ProtocolProviderFactory.USER_ID)
            + "))";

    try {
      serRefs =
          JabberSlickFixture.bc.getServiceReferences(
              ProtocolProviderService.class.getName(), osgiFilter);
    } catch (InvalidSyntaxException ex) {
      // this really shouldhn't occur as the filter expression is static.
      fail(osgiFilter + "is not a valid osgi filter");
    }

    assertTrue(
        "An protocol provider was apparently not installed as " + "requested.",
        serRefs != null && serRefs.length > 0);

    Object jabberProtocolProvider = JabberSlickFixture.bc.getService(serRefs[0]);

    assertTrue(
        "The installed protocol provider does not implement " + "the protocol provider service.",
        jabberProtocolProvider instanceof ProtocolProviderService);
  }
Exemple #3
0
  /**
   * Starts recording {@link #call} creating {@link #recorder} first and asking the user for the
   * recording format and file if they are not configured in the "Call Recording" configuration
   * form.
   *
   * @return <tt>true</tt> if the recording has been started successfully; otherwise, <tt>false</tt>
   */
  private boolean startRecording() {
    String savedCallsPath = configuration.getString(Recorder.SAVED_CALLS_PATH);
    String callFormat;

    // Ask the user where to save the call.
    if ((savedCallsPath == null) || (savedCallsPath.length() == 0)) {
      /*
       * Delay the initialization of callFileChooser in order to delay the
       * creation of the recorder.
       */
      if (callFileChooser == null) {
        callFileChooser =
            GenericFileDialog.create(
                null,
                resources.getI18NString("plugin.callrecordingconfig.SAVE_CALL"),
                SipCommFileChooser.SAVE_FILE_OPERATION);
        callFileChooser.addFilter(
            new SipCommFileFilter() {
              @Override
              public boolean accept(File f) {
                return f.isDirectory() || isSupportedFormat(f);
              }

              @Override
              public String getDescription() {
                StringBuilder description = new StringBuilder();

                description.append("Recorded call");

                Recorder recorder;

                try {
                  recorder = getRecorder();
                } catch (OperationFailedException ofex) {
                  logger.error("Failed to get Recorder", ofex);
                  recorder = null;
                }
                if (recorder != null) {
                  List<String> supportedFormats = recorder.getSupportedFormats();

                  if (supportedFormats != null) {
                    description.append(" (");

                    boolean firstSupportedFormat = true;

                    for (String supportedFormat : supportedFormats) {
                      if (firstSupportedFormat) firstSupportedFormat = false;
                      else description.append(", ");
                      description.append("*.");
                      description.append(supportedFormat);
                    }

                    description.append(')');
                  }
                }
                return description.toString();
              }
            });
      }
      // Offer a default name for the file to record into.
      callFileChooser.setStartPath(createDefaultFilename(null));

      File selectedFile = callFileChooser.getFileFromDialog();

      if (selectedFile != null) {
        callFilename = selectedFile.getAbsolutePath();

        /*
         * If the user specified no extension (which seems common on Mac
         * OS X at least) i.e. no format, then it is not obvious that we
         * have to override the set Recorder.CALL_FORMAT.
         */
        callFormat = SoundFileUtils.getExtension(selectedFile);

        if ((callFormat != null) && (callFormat.length() != 0)) {
          /*
           * If the use has specified an extension and thus a format
           * which is not supported, use a default format instead.
           */
          if (!isSupportedFormat(selectedFile)) {
            /*
             * If what appears to be an extension seems a lot like
             * an extension, then it should be somewhat safer to
             * replace it.
             */
            if (SoundFileUtils.isSoundFile(selectedFile)) {
              callFilename = callFilename.substring(0, callFilename.lastIndexOf('.'));
            }
            String configuredFormat = configuration.getString(Recorder.FORMAT);
            callFormat =
                (configuredFormat != null && configuredFormat.length() != 0)
                    ? configuredFormat
                    : SoundFileUtils.DEFAULT_CALL_RECORDING_FORMAT;

            callFilename += '.' + callFormat;
          }
          configuration.setProperty(Recorder.FORMAT, callFormat);
        }
      } else {
        // user canceled the recording
        return false;
      }
    } else {
      callFilename = createDefaultFilename(savedCallsPath);
      callFormat = SoundFileUtils.getExtension(new File(callFilename));
    }

    Throwable exception = null;

    try {
      Recorder recorder = getRecorder();

      if (recorder != null) {
        if ((callFormat == null) || (callFormat.length() <= 0))
          callFormat = SoundFileUtils.DEFAULT_CALL_RECORDING_FORMAT;

        recorder.start(callFormat, callFilename);
      }

      this.recorder = recorder;
    } catch (IOException ioex) {
      exception = ioex;
    } catch (MediaException mex) {
      exception = mex;
    } catch (OperationFailedException ofex) {
      exception = ofex;
    }
    if ((recorder == null) || (exception != null)) {
      logger.error(
          "Failed to start recording call " + call + " into file " + callFilename, exception);
      return false;
    } else return true;
  }