2
\$\begingroup\$

I've put together a function in Delphi XE7 which returns the Dropbox directory path on my machine, as this path can be customized by the user.

This function currently works on my computer, but it could have many defects preventing it to work for other users on different machines. So I would be very grateful if anybody could point to these defects.

uses
  System.IOUtils, System.JSON, System.NetEncoding, JclSysInfo, JclStrings

function GetDropboxPath: string;
var
  tempstr: string;
  function ParseJsonStr(jStr, jPar1, jPar2: string): string; 
  var
    jObj: TJSONObject;
    jVal: TJSONValue;
  begin
    Result:= '';
    jObj := TJSONObject.ParseJSONValue(jStr) as TJSONObject;
    try
      jVal:= jObj.Get(jPar1).JsonValue;
      Result:= TJSONObject(jVal).Get(jPar2).JsonValue.Value;
    finally
      jObj.Free;
    end;
  end;
begin
  Result := '';
  tempstr := IncludeTrailingBackslash(JclSysInfo.GetAppdataFolder) + 'Dropbox\'; 
  if DirectoryExists(tempstr) then
  begin    
    if FileExists(tempstr + 'info.json') then // try with info.json
    begin
      tempstr := tempstr + 'info.json';
      tempstr := System.IOUtils.TFile.ReadAllText(tempstr);
      try
        tempstr := ParseJsonStr(tempstr, 'personal', 'path');
      except
        // Todo: log this error
        EXIT;
      end;
      Result := Trim(tempstr);
    end    
    else if FileExists(tempstr + 'host.db') then // else try with host.db
    begin
      tempstr := tempstr + 'host.db';
      tempstr := Trim(JclStrings.StrTrimCharLeft(System.IOUtils.TFile.ReadAllText(tempstr), '0'));
      Result := Trim(TNetEncoding.Base64.Decode(tempstr)); // needs XE7
    end;
  end;
end;
\$\endgroup\$
6
  • \$\begingroup\$ The result of the subroutine ParseJsonStrcould be an empty string. In this case the host.db file should be checked, which currently is not the case because of the if-else structure. So instead of the if-else structure at the end of the info.json check, the result should be evaluated. Only in the case (and only then) it is empty, the second check of the host-db file should be executed. What do you think? \$\endgroup\$ Commented Feb 23, 2015 at 15:49
  • \$\begingroup\$ Also, in the except section, there should be no EXIT but instead it should continue to the host.db check. What do you think? \$\endgroup\$ Commented Feb 23, 2015 at 15:57
  • \$\begingroup\$ Personally, I would be reading that path from the registry. Any user can install DropBox wherever he wants to. I doubt that you can rely on JclSysInfo.GetAppdataFolder (and, even if it works for DropBox, because they hard code some stuff, instead of following MS rules & allowing a choice, it is not guaranteed to work for all (read "properly coded") apps). \$\endgroup\$ Commented Apr 13, 2015 at 14:19
  • \$\begingroup\$ @Mawg From where in the Registry would you read it? \$\endgroup\$ Commented Apr 14, 2015 at 18:50
  • \$\begingroup\$ Google is your friend (or you could even just fire up Regedit & search for DropBox). See forensicartifacts.com/2011/07/dropbox-config-files-windows or forensicfocus.com/Content/pid=429/page=2 or any of the other numerous results of googling for dropbox registry keys \$\endgroup\$ Commented Apr 15, 2015 at 12:38

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.