 
   # Handlers
   # --------
-
-  _handleBuild: (err, res, body, server) =>
+  _handleNewFolder: (err, res, body, server, folder) =>
+    if err
+      @send err
+      return
+
+    try
+      content = JSON.parse(body)
+      @_addJobsToFoldersList content.jobs, server, folder
+      @_initComplete() if @_serverManager.hasInitialized()
+    catch error
+      @send error
+
+  _handleBuild: (err, res, body, server, folder) =>
     if err
       @reply err
     else if 200 <= res.statusCode < 400 # Or, not an error code.


  describe: =>
    return if not @_init(@describe)
    job = @_getJob(true)
    server = @_serverManager.getServerByJobName(job)
    if !server
      @msg.send "I couldn't find any servers with a job called #{@_getJob()}.  Try `jenkins servers` to get a list."
      return
    @_requestFactorySingle server, "job/#{job}/api/json", @_handleDescribe

  getAlias: =>
    aliases    = @_getSavedAliases()

  url: null
  auth: null
  _hasListed: false
  _rootFolder: null
  _querystring: null

  constructor: (url, auth) ->
    @url = url
    @auth = auth
    @_querystring = require 'querystring'

  hasInitialized: ->
    @_hasListed

  setFolder: (folder) =>
    @_hasListed = true
    @_rootFolder = folder

  getFolder: =>
    @_rootFolder

  hasFolder: =>
    true if @_rootFolder

  getFolderByName: (folderName) =>
    @_rootFolder.getFolderByName(folderName)

  hasFolderByName: (folderName) =>
    @_rootFolder.hasFolderByName(folderName)

  getJobByName: (jobName) =>
    @_rootFolder.getJobByName(jobName)

  hasFolderByName: (jobName) =>
    @_rootFolder.hasJobByName(jobName)

class JenkinsFolder
  name: null
  path: null
  depth: null
  _jobs: null
  _folders: null
  _querystring: null

  constructor: (name, path, depth) ->
    @name = name
    @path = path
    @depth = depth
    @_jobs = []
    @_folders = []
    @_querystring = require 'querystring'

  hasInitialized: ->
    @_hasListed

  addJob: (job) =>
    @_hasListed = true
    @_jobs.push job if not @hasJobByName(job.name, false)

  getJobs: =>
    @_jobs
